0

I'm trying to write a JavaScript server running on NodeJS using Socket.io that communicates with the client which is written in JAVA.

I have no problem with the server side because I try it with a JavaScript client, so I assumed that the problem came from my java client.

I have this error on my client side :

io.socket.SocketIOException: Error while handshaking
    at io.socket.IOConnection.handshake(IOConnection.java:322)
    at io.socket.IOConnection.access$600(IOConnection.java:39)
    at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:8000/socket.io/1/at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at io.socket.IOConnection.handshake(IOConnection.java:313)

This is my code(client side) :

public class BasicExample implements IOCallback {
    private SocketIO socket;

    public static void main(String[] args) {
        try {
            new BasicExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public BasicExample() throws Exception {
        socket = new SocketIO();
        socket.connect("http://localhost:8000", this);

        // Sends a string to the server.
        socket.send("Hello Server");

        // Emits an event to the server.
        socket.emit("event", "ILYES");
    }

    @Override
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(String data, IOAcknowledge ack) {
        System.out.println("Server said: " + data);
    }

    @Override
    public void onError(SocketIOException socketIOException) {
        System.out.println("an Error occured");
        socketIOException.printStackTrace();
    }

    @Override
    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }

    @Override
    public void onConnect() {
        System.out.println("Connection established");
    }

    @Override
    public void on(String event, IOAcknowledge ack, Object... args) {
        System.out.println("Server triggered event '" + event + "'");
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
ilyesmilano
  • 37
  • 11
  • Wich version of node.js do you use? According to this post the Gottox client needs an older version of node.js (e.g. 0.9.16): https://stackoverflow.com/questions/24081179/gottox-socket-io-java-client-error-while-handshaking-caused-by-java-io-filenot – Tobi Tiggers Apr 24 '18 at 06:07
  • the node version is **v8.9.4** and the socket.io version is **2.1.0** – ilyesmilano Apr 24 '18 at 15:08

0 Answers0