如何使用 OkHttp 实现 websocket
1.简介
在分析 OkHttp 源码结构的时候,常常可以发现 WebSocket 的英文、变量、参数及方法。查阅官方数据手册,也有说明:
OkHttp v3.5 now includes support for bidirectional web sockets. The URL that should be used should be prefixed with ws:// or wss:// for the secure version. Although the connection ports are the same as HTTP (port 80 and port 443), the server still needs to be configured to support WebSockets since they are a completely different protocol.
2.代码
复制代码
3.总结
往 WebSocket 中发送数据
复制代码
关闭 WebSocket
复制代码
Demo 输出的日志
复制代码
版权声明: 本文为 InfoQ 作者【Changing Lin】的原创文章。
原文链接:【http://xie.infoq.cn/article/260ca5f633738fb1e9902729d】。文章转载请联系作者。
评论