我已经将此库用于套接字。
implementation ('com.github.nkzawa:socket.io-client:0.3.0',{
exclude group:'org.json', module:'json'
})
我发现了许多解决WebSocket连接问题的方法,但仍然面临着这个问题。
Fatal Exception: org.java_websocket.exceptions.WebsocketNotConnectedException
org.java_websocket.WebSocketImpl.send (WebSocketImpl.java:566)
org.java_websocket.WebSocketImpl.send (WebSocketImpl.java:543)
org.java_websocket.client.WebSocketClient.send (WebSocketClient.java:171)
com.github.nkzawa.engineio.client.transports.WebSocket$2.call (WebSocket.java:114)
com.github.nkzawa.engineio.parser.Parser.encodePacket (Parser.java:63)
com.github.nkzawa.engineio.parser.Parser.encodePacket (Parser.java:42)
com.github.nkzawa.engineio.client.transports.WebSocket.write (WebSocket.java:110)
com.github.nkzawa.engineio.client.Transport$3.run (Transport.java:98)
com.github.nkzawa.thread.EventThread.exec (EventThread.java:50)
com.github.nkzawa.engineio.client.Transport.send (Transport.java:94)
com.github.nkzawa.engineio.client.Socket.flush (Socket.java:596)
com.github.nkzawa.engineio.client.Socket.sendPacket (Socket.java:681)
com.github.nkzawa.engineio.client.Socket.sendPacket (Socket.java:655)
com.github.nkzawa.engineio.client.Socket.access$1500 (Socket.java:29)
com.github.nkzawa.engineio.client.Socket$17.run (Socket.java:565)
com.github.nkzawa.thread.EventThread.exec (EventThread.java:50)
com.github.nkzawa.engineio.client.Socket.ping (Socket.java:562)
com.github.nkzawa.engineio.client.Socket$16$1.run (Socket.java:550)
com.github.nkzawa.thread.EventThread$2.run (EventThread.java:75)
答案 0 :(得分:0)
https://github.com/socketio/socket.io-client-java
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
builder.header("Accept", "application/json")
return chain.proceed(builder.build());
}
})
.addInterceptor(new com.ihsanbal.logging.LoggingInterceptor.Builder()
.loggable(true)
.setLevel(Level.BASIC)
.log(Platform.INFO)
.request("Request")
.response("Response")
.build());
okhttp3.OkHttpClient okHttpClient = httpClient.build();
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
if (mSocket == null || isClosed) {
io.socket.client.Socket mSocket = IO.socket("http://yorurl.com", mOptions);
}
mSocket.on(Socket.EVENT_CONNECT, args -> {
if (!isConnected) {
Log.i(TAG, "onSocketConnected");
isConnected = true;
isClosed = false;
}
});
mSocket.on(Socket.EVENT_DISCONNECT, args -> {
Log.i(TAG, "onDisconnected");
isConnected = false;
});
mSocket.on(Socket.EVENT_ERROR, args -> {
Log.i(TAG, "onConnectError");
isConnected = false;
});
mSocket.on(Socket.EVENT_CONNECT_ERROR, args -> {
Log.i(TAG, "onConnectError");
isConnected = false;
});
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, args -> {
Log.i(TAG, "onConnectTIMEOUT");
isConnected = false;
});
if (!mSocket.connected() && !isConnected) {
mSocket.connect();
}