Reputation: 139
I searched for a long time but could not find a solution.
I build a node.js server which client can change "rooms" to get different information.
Everytime client socket leave/join between rooms, I log (socket.adapter.rooms or socket.rooms) ,it shows client in only one room (and itself), looks right.
The problem is when server send specific messages to specific room members, client receive duplicate message.. Each time a join/leave operation is performed, the message is repeated one more time.
for example: first client join "715R" room, client recive message from server,the message is only sent once, correct. then client leave "715R" and join "312R" , now client receive "312R"'s message repeat twice!... then client leave "312R" and join "715R", now client receive "715R"'s message repeat three times!...
but socket.rooms/socket.adapter.rooms log are showing only in one room.
my server side code:
socket.on('join', function (msg) {
console.log(socket.rooms);
console.log('client:'+socket.id+" ask join:"+msg);
if (typeof socket['room'] !== 'undefined') {
console.log(socket.id + " try leave " +socket['room']);
socket.leave(socket['room'], function() {
console.log('client:'+socket.id+" leave ok:"+socket['room']);
console.log(socket.adapter.rooms);
delete socket['room'];
console.log(socket.id + " inRoom: " + socket['room']);
console.log(socket.id + " try join:" + msg);
if (checkroom(rooms, msg)) {
socket.join(msg, function() {
console.log(socket.id + " join sucess:" + msg);
socket['room'] = msg;
console.log(socket.adapter.rooms);
console.log('-------------------------');
});
}
});
}else {
console.log(socket.id + " inRoom: " + socket['room']);
console.log(socket.id + " try join:" + msg);
if (checkroom(rooms, msg)) {
socket.join(msg, function() {
console.log(socket.id + " join sucess:" + msg);
socket['room'] = msg;
console.log(socket.adapter.rooms);
console.log('-------------------------');
});
}
}
Upvotes: 0
Views: 1560
Reputation: 139
I solve the problem. Not the "room" related issue. Because each client room has a copy of socket.io listener (socket.on(....)) . so when client switch room, the client room page repeated the registration. When Server send message to client ,the same client socket ID has many listener to receive the data.The issue appear. Solution: let listener has only one instance then pass data to every room page client want.
Upvotes: 1