0

I have a socketio server running and I want to interact with it with Java. I know that there is an official client, but I couldn't get it to work for a regular java project, so i'm trying to see if I can interact with it through regular sockets. Here's what I have so far:

Socket sock = new Socket("127.0.0.1", 5000); 
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter outToServer = new PrintWriter(sock.getOutputStream(), true);

outToServer.println("connect");

while(true){
    while(inFromServer.ready()){
        inFromServer.readLine();
    }
}

It appears to connect, but I don't get any response from neither the client or server. What exactly do I need to send to get a response from the server? And what information will I get back?

Caleb Lewis
  • 523
  • 6
  • 20

1 Answers1

1

socket.io is its own extension on top of webSockets which is its own protocol on top of a plain socket.

You will have to implement both webSockets and socket.io in order to talk to a socket.io server. Unless this is an academic exercise for learning only, I'd strongly suggest you get a socket.io library that works in Java and use that as it will save you a lot of time, problems and bugs.

Otherwise, you have multiple specifications to study and multiple protocols to implement. FYI, a websocket connection starts with an HTTP request that is upgraded to the webSocket protocol. Once you have a webSocket connection established, you can then send socket.io messages over it.

For the basics of the webSocket protocol, I found this to be a useful read: https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_servers

jfriend00
  • 683,504
  • 96
  • 985
  • 979