'Java Socket.IO-client SEVERE: Task threw exception

I try to connect to Web Socket server by using Socket.IO-client Java

First i tried this config:

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 
}

And got this:

enter image description here

Here i found some hint. After i added:

    options.transports = arrayOf(WebSocket.NAME)

The new error appear

> 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.access$1000(Socket.java:36)
    at io.socket.engineio.client.Socket$5.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.access$100(WebSocket.java:23)
    at io.socket.engineio.client.transports.WebSocket$1$2.run(WebSocket.java:73)
    at io.socket.thread.EventThread$2.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.access$1000(Socket.java:36)
    at io.socket.engineio.client.Socket$5.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.access$100(WebSocket.java:23)
    at io.socket.engineio.client.transports.WebSocket$1$2.run(WebSocket.java:73)
    at io.socket.thread.EventThread$2.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

This is Minimal reproducible example:

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
}

Do not forget add in gradle

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


Solution 1:[1]

Problem was that I did run this without android device. If I run it in android studio inside some .apk everything is ok. But if I run inside fun main() I get this error. By the way this code works if run it by Idea ide inside main() method. This is my final code:

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);
}

Don't forget add:

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

Hope it will save your time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snailp4el