WebSocket连接不上wss?Websocket 链接wss报secure WebSockets not implemented?
不存在的。
经测试使用多个开源框架后发现,还是google给的最给力
引入okhttp,这里就有websocket的开源啊!!!
接下来上一段链接Websocket 的代码(😁也是为了方便以后自己不再造轮子):
//新建client
client = new OkHttpClient.Builder().build();
//构造request对象
Request request = new Request.Builder().url(AppConfig.SOCKET_NET_HOST).build();
//建立连接
client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
mWebSocket = webSocket;
String stringBuffer = "client onOpen" + "\n" +
"client request header:" + response.request().headers() + "\n" +
"client response header:" + response.headers() + "\n" +
"client response:" + response + "\n";
android.util.Log.v("sss","onOpen=\n" + stringBuffer);
//到了这里就说明socket链接成功了,我这里因项目需求 需再做一个登录操作。
loginSocket();
socketIsConnect = true;
}
@Override
public void onMessage(WebSocket webSocket, String text) {
Message message = Message.obtain();
message.obj = text;
handler.sendMessage(message);
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
String stringBuffer = "client onClosing" + "\n" +
"code:" + code + " reason:" + reason + "\n";
android.util.Log.v("sss","onClosing=\n" + stringBuffer);
socketIsConnect = false;
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
String stringBuffer = "client onClosing" + "\n" +
"code:" + code + " reason:" + reason + "\n";
android.util.Log.v("sss","onClosing=\n" + stringBuffer);
socketIsConnect = false;
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
String stringBuffer = "client onFailure" + "\n" +
"throwable:" + t + "\n" +
"response:" + response + "\n";
android.util.Log.v("sss","onFailure=\n" + stringBuffer);
socketIsConnect = false;
}
});
网友评论