Server:
var app = require('http').createServer(function(req,res){}); app.listen(3250); var io = require('socket.io').listen(app) //io = socketio.listen(server); // handle incoming connections from clients io.sockets.on('connection', function(socket) { // once a client has connected, we expect to get a ping from them saying what room they want to join socket.on('room', function(room) { socket.join(room); }); socket.on('say',function(data){ io.sockets.in(data.room).emit('message',data.message); }) });
customer:
<!DOCTYPE HTML> <html> <head> <script src="http://192.168.1.7:3250/socket.io/socket.io.js"></script> <script> </script> </head> <body> <button onclick='socket.emit("say",{"room":room,"message":"hello world"})'>Say hello world</button> </body> </html>
change 192.168.1.7 to your IP address, good luck!
source share