alexandrainst / processing_websockets

A web socket library, including both server and client, for Processing
MIT License
91 stars 36 forks source link

sendMessage to SocketIO Server doesnt work. [FIXED] #3

Open omarojo opened 7 years ago

omarojo commented 7 years ago

Im trying to send a message to a SocketIO server. Receiving messages from the server works fine. But if I try your sample demo, the program breaks trying to send a message. It almost seem like the socket connection gets disconnected after trying to send a Message.

org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]

omarojo commented 7 years ago

FIXED... Just in case anyone is having troubles connecting to SocketIO server. You need to use a specific url to connect. And if you want to send messages from processing to the server, you need to concat the number "42" at the beginning of the string as well as format the message in a array manner, where the first element is the name of message the server should subscribe to.

The following is an incomplete processing sketch just to demonstrate the changes I had to do with the socket message parsing, sending an receiving as well as the url format.

void setup(){
  size(1024,768);

  newEllipse=false;

  wsc= new WebsocketClient(this, "ws://localhost:8080/socket.io/?EIO=3&transport=websocket");
  remotePosition = new PVector(0,0);
  now=millis();
}

void draw(){
  if(newEllipse){
    if(remotePosition != null)
      ellipse(remotePosition.x,remotePosition.y,10,10);
    newEllipse=false;
  }

  if(millis()>now+5000){
    try{
      wsc.sendMessage("42[\"message\",{\"msg\":\"hello from processing\"}]");
    }catch (Exception e){
      println(e);
    }
    now=millis();
  }
}
void webSocketEvent(String msg){
 println("- " + msg);
  if(msg.length() >= 3){
    String numKey = msg.substring(0,2);
    String stringJson = msg.substring(2,msg.length());
    if(numKey.equals("42") ){
      String[] result = stringJson.split("\"");
      String[] result2 = stringJson.split(",");
      String method = result[1];

      String allValues = "";
      for(int i = 1; i < result2.length ; i++){
        if(allValues.equals(""))
          allValues = result2[i];
        else
          allValues = allValues +","+ result2[i];
      }
      allValues = allValues.substring(0,allValues.length()-1); //remove the ] at the end
      stringJson = "{\"method\":\""+method+"\",\"value\":"+allValues+"}";

      JSONObject json = parseJSONObject(stringJson);
      println(json);
      processJson(json); //Process the JSON object somewhere else to do some magic with it
    }
  }
}

in your NodeJS socketio server

io.sockets.on('connection', function (socket) {
  console.log("Someone connected");
  io.to(socket.id).emit('connected', 'yeah!');
  socket.once('disconnect', function () {
    console.log("Someone Disconnected");
  });
  socket.on('message', function(data){
    console.log(data);
  });
});

Reference Info: http://stackoverflow.com/questions/23987640/socket-io-handshake-return-error-transport-unknown