javascript - ( Socket.io ) One socket connection multiple rooms -
i'm having problems socket.io. try create single socket connection connected multiple rooms.
this current code:
function joinroom(id){ socket = io(domain); socket.on('connect', function (data) { console.log('connected ' + room); socket.emit('room', room); }); socket.on('message', function (data) { console.log(data); }); }
the problem if remove var socket.io(domain) function not connected , not receive data room.
example:
socket = io(domain); function joinroom(id){ socket.on('connect', function (data) { console.log('connected ' + room); socket.emit('room', room); }); socket.on('message', function (data) { console.log(data); }); }
if take socket.io() function of joinroom() function not receive messages message or anything. not work. doing wrong? solution?
on client, create event indicate access room.
on server, apply access logic.
client
var socket = io('http://localhost'); function joinroom(data){ socket.emit("join", data); } var data = { room:'ejemplo' }; joinroom(data);
server
var io = require('socket.io')(app); io.on('connection', function(socket){ socket.on('join', function(data){ console.log(data); // data = { room: string, ...} socket.join(data.room); }); socket.on('leave', function(data){ socket.leave(data.room); });
Comments
Post a Comment