Processing to connect to websocket - Nodejs/Socket.io server

edited December 2013 in Library Questions

I have a very simple nodejs server running with socket.io. This server is waiting to hear messages about a users mouse position from the client:

socket.on('connection', function (client) {
                client.on('MOVE', function(data){
                        console.log(data);
                });
});

Using a webpage as my client - all I need to do in order to send this server my mouse position is:

socket.emit('MOVED', mouseX);

This all works in the browser, but I'd like to ultimately have Processing as my client. I've tried the following, but I always receive "Client got end-of-stream." which indicates to me that I can't send it information:

import processing.net.*;
Client c;
String data;

void setup() {
  size(200, 200);
  c = new Client(this, "myPublicIPAdress", 8080);
  c.write("GET / HTTP/1.0\r\n");
  c.write("\r\n");
}

void draw() {
  if (c.available() > 0) {
    data = c.readString();
    println(data);
  }
  c.write("MOVE, " + mouseX);
}

Along with receiving the end-of-stream notice, my nodejs server never acknowledges that the Processing client has hit it. In order to try to solve these issues, I added in my HTTP server head "Connection: keep-alive". Doing this allows the Processing code to write the c.readString() before it receives the "Client got end-of-stream" notice.

My questions are:

1) How can I connect to my nodejs server and continuously send socket information using Processing?

2) Once connected, what is the syntax for emitting data to a nodejs server via Processing? I doubt node will be able to read the event with: c.write("MOVE, " + mouseX);

Thanks very much for helping. I did a lot of googling with no solution found.

Answers

  • edited December 2013

    Found my own answer on SO: http://stackoverflow.com/questions/18900187/processing-how-to-send-data-through-websockets-to-javascript-application

    It would be awesome if processings .net library supported this as well. Until then, this java_websocket library works great :)

  • It would be great to share code!

    I have an Arduino + Ethernet that pulls sensor data to UDP socket in a server, then a iosocket is able to show real time data in a webpage. I just wanted to use processing to do the same, connect to the iosocket and show the latest updated data (temperature sensor).

    I've followed the tutorial from stackoverflow... i can't connect to the socket with ws:// ..

  • Hey - I haven't had time to upload my code to github yet, but I can tell you how I got this to work.

    I found the .jar file (found in /dist) from https://github.com/TooTallNate/Java-WebSocket and placed it in my Processing directory: “Processing / libraries / java_websocket / library / java_websocket.jar”. Then I took the the client code noted on the Stack Overflow post. Running this, I kept receiving ‘destroying non-socket.io upgrade’ on my nodejs server. In order to get around this, I switched my nodejs server from the socketio library to the ws websocket library. This allowed everything to run smoothly.

  • @rob0738 it is great you managed to get a websocket connection with Processing, but the whole point I think, it was to keep using socket.io which is great module for NodeJS I wouldnt like to change socket.io with other library.

Sign In or Register to comment.