billroy / socket.io-arduino-client

A socket.io client for the Arduino Ethernet shield
MIT License
195 stars 66 forks source link

How do I relay what the arduino sent to SocketIO back to other connected browsers #2

Closed 4werd closed 11 years ago

4werd commented 11 years ago

Bill,

Thank you so much for making this Arduino/SockIO code.

I can see the Arduino is sending data to my socketIO server in my mac osx terminal window (see attached screen shot) but I'm having trouble figuring out how to then get my server to relay it back out to the other connected browsers?

I'm guessing the syntax or something is incorrect on my server code:

//Server

io.sockets.on('connection', function (socket) { socket.send("Hi from Server"); socket.on('message', function (data) { socket.send(data); }); });

//And here's my Client code

var socket = io.connect('http://10.0.1.3:80');

socket.on('message', function (data) { $('#measurementlabel').html(data); });

I appreciate any help you can offer in figuring it out. Hopefully it's something simple that I've missed.

Thanks,

Drew

billroy commented 11 years ago

In relaying to the connected web browsers it is necessary to somehow distinguish them from the connected arduino(s).

The example index.js server and index.html client (in the examples/bitlashsocketio/ directory) arrange to do this by putting the browser traffic into a separate socket.io namespace named 'tty'.

In index.js around line 63 you'll see the code that sets up the tty namespace and its listener for the browser connections. Then at line 85 you'll see this code in the arduino message listener to send the data to the message channel on the browser tty namespace:

tty.emit('message', data);

In a corresponding way the setup code in the example index.html at line 29 connects to and uses the browser namespace:

var tty = io.connect('http://localhost:3000/tty');
console.log('Socket connected', tty);
tty.on('message', function(data) { 
    data = data.replace(/\n/g, '<br/>');
    data = data.replace(/ /g, '&nbsp;');
    append(data);
});

Perhaps something similar to the examples would do what you need.

-br

On Jun 13, 2013, at 2:06 PM, 4werd wrote:

Bill,

Thank you so much for making this Arduino/SockIO code.

I can see the Arduino is sending data to my socketIO server in my mac osx terminal window (see attached screen shot) but I'm having trouble figuring out how to then get my server to relay it back out to the other connected browsers?

I'm guessing the syntax or something is incorrect on my server code:

//Server

io.sockets.on('connection', function (socket) { socket.send("Hi from Server"); socket.on('message', function (data) { socket.send(data); }); });

//And here's my Client code

var socket = io.connect('http://10.0.1.3:80');

socket.on('message', function (data) { $('#measurementlabel').html(data); });

I appreciate any help you can offer in figuring it out. Hopefully it's something simple that I've missed.

Thanks,

Drew

— Reply to this email directly or view it on GitHub.

4werd commented 11 years ago

Bill,

Thanks for the quick reply. I'll update my code accordingly.

Thanks,

Drew

4werd commented 11 years ago

Bill,

Thanks for the advice on the socket.io namespace, my Arduino and SocketIO server are communicating now.

Thanks,