Socket.io rooms doesn't work
javascript, node.js, socket.io
Solution
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);
})
});
client:
<!DOCTYPE HTML>
<html>
<head>
<script src="http://192.168.1.7:3250/socket.io/socket.io.js"></script>
<script>
// set-up a connection between the client and the server
var socket = io.connect('http://192.168.1.7:3250');
// let's assume that the client page, once rendered, knows what room it wants to join
var room = "abc123";
socket.on('connect', function() {
// Connected, let's sign-up for to receive messages for this room
socket.emit('room', room);
});
socket.on('message', function(data) {
console.log('Incoming message:', data);
});
</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!
Problem
I have problem because this example don't work and i don't know why... Server ``` var app = require('http').createServer(handler); var io = require('socket.io').listen(app); var fs = require('fs'); var port = 3250; app.listen(port); io.sockets.on('connection', function(socket) { socket.on('room', function(room) { socket.join(room); }); var room = abc123 io.sockets.in(room).emit('m', 'what is going on, party people?'); }); ``` Client ``` var room = "abc123"; socket.on('connect', function() { socket.emit('room', room); }); socket.on('m', function(data) { document.write(data); }); ``` I'm trying with this https://gist.github.com/crabasa/2896891 and it not helped :( Thank you for help!