Java Socket.IO-客户端严重:任务抛出异常

Java Socket.IO-client SEVERE: Task threw exception

我尝试使用 Socket.IO-client Java

连接到 Web Socket 服务器

首先我尝试了这个配置:

fun createSocket(): Socket {
    val options = IO.Options()
    options.path = "/ws"
    options.reconnectionAttempts = 1
    val socket = IO.socket("http://93.90.222.147:8087" , options)
    return socket 
}

得到这个:

在这里我找到了一些 。我添加后:

    options.transports = arrayOf(WebSocket.NAME)

出现新错误

> SEVERE: Task threw exception
java.lang.RuntimeException: Stub!
    at org.json.JSONObject.<init>(JSONObject.java:115)
    at io.socket.engineio.client.HandshakeData.<init>(HandshakeData.java:16)
    at io.socket.engineio.client.Socket.onPacket(Socket.java:541)
    at io.socket.engineio.client.Socket.access00(Socket.java:36)
    at io.socket.engineio.client.Socket.call(Socket.java:337)
    at io.socket.emitter.Emitter.emit(Emitter.java:117)
    at io.socket.engineio.client.Transport.onPacket(Transport.java:124)
    at io.socket.engineio.client.Transport.onData(Transport.java:116)
    at io.socket.engineio.client.transports.WebSocket.access0(WebSocket.java:23)
    at io.socket.engineio.client.transports.WebSocket.run(WebSocket.java:73)
    at io.socket.thread.EventThread.run(EventThread.java:80)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
> 
> Exception in thread "EventThread" java.lang.RuntimeException: Stub!
    at org.json.JSONObject.<init>(JSONObject.java:115)
    at io.socket.engineio.client.HandshakeData.<init>(HandshakeData.java:16)
    at io.socket.engineio.client.Socket.onPacket(Socket.java:541)
    at io.socket.engineio.client.Socket.access00(Socket.java:36)
    at io.socket.engineio.client.Socket.call(Socket.java:337)
    at io.socket.emitter.Emitter.emit(Emitter.java:117)
    at io.socket.engineio.client.Transport.onPacket(Transport.java:124)
    at io.socket.engineio.client.Transport.onData(Transport.java:116)
    at io.socket.engineio.client.transports.WebSocket.access0(WebSocket.java:23)
    at io.socket.engineio.client.transports.WebSocket.run(WebSocket.java:73)
    at io.socket.thread.EventThread.run(EventThread.java:80)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
EVENT_CONNECT_ERROR: [Ljava.lang.Object;@6104f122

这是最小的可重现示例:

fun main() {
    val socket = createSocket()
    socket.on(Socket.EVENT_CONNECT_ERROR) {
        println("EVENT_CONNECT_ERROR: " + it.toString())
    }
    socket.connect()
}

fun createSocket(): Socket {
    val options = IO.Options()
    options.transports = arrayOf(WebSocket.NAME)
    options.path = "/ws"
    options.reconnectionAttempts = 1
    val socket = IO.socket("http://93.90.222.147:8087" , options)
    return socket
}

别忘了加入gradle

    implementation ('io.socket:socket.io-client:2.0.1') {
    exclude group: 'org.json', module: 'json'
}

问题是我在没有 android 设备的情况下 运行 做到了这一点。如果我 运行 它在 android 工作室 inside 一些 .apk 一切正常。但是如果我 运行 inside fun main() 我得到这个错误。顺便说一下,如果 运行 通过 Idea ide inside main() 方法,这段代码就可以工作。这是我的最终代码:

public Socket createSocket2(String token) throws URISyntaxException {
    IO.Options options = new IO.Options();
    options.transports = new String[] {WebSocket.NAME};
    options.path = "/ws";
    options.reconnectionAttempts = 1;
    options.extraHeaders = singletonMap("Authorization", singletonList(token));
    return IO.socket("http://8.8.8.8:8087", options);
}

不要忘记添加:

<uses-permission android:name="android.permission.INTERNET" />

希望它能节省您的时间。