Reputation:
When the user joins, I change his ID to a shorter randomized code. This is for the purpose of using the id as a front-end-visible session ID, I mention this simply because otherwise you might be confused why my socket IDs aren't standard, and it could even be related to the problem.
Then a room is created with that code/id as its name, here's the relevant code from the session creation event socket handler, where client
is what most people call socket
and socket
is what most people call io
, I just named them differently, so hopefully that's clear so far:
var sessionId = client.id;
debug.log('Session ID: ' + sessionId);
debug.log('Data: ' + JSON.stringify(data, null, 3));
debug.log(JSON.stringify(socket.sockets.adapter.rooms));
client.join(sessionId);
debug.log(JSON.stringify(socket.sockets.adapter.rooms));
callback(sessions[sessionId]);
So at this point, we get a log which demonstrates that the client is a member of the new socket room by looking at the socket.sockets.adapter.rooms
object (one shows before joining the room and the other shows after):
[Debug][Create Session Event Handler]: Session ID: QXVLSLE
[Debug][Create Session Event Handler]: Data: {
"name": "Jonathan"
}
[Debug][Create Session Event Handler]: {"/#0JCm5cyTpqTkXTIoAAAA":{"sockets":
{"/#0JCm5cyTpqTkXTIoAAAA":true},"length":1},"/#pqG0tzoc4i7xg8ygAAAB":{"sockets":
{"/#pqG0tzoc4i7xg8ygAAAB":true},"length":1},"/#R9HQ2k6cOqF_bodmAAAC":{"sockets":
{"/#R9HQ2k6cOqF_bodmAAAC":true},"length":1}}
[Debug][Create Session Event Handler]: {"/#0JCm5cyTpqTkXTIoAAAA":{"sockets":
{"/#0JCm5cyTpqTkXTIoAAAA":true},"length":1},"/#pqG0tzoc4i7xg8ygAAAB":{"sockets":
{"/#pqG0tzoc4i7xg8ygAAAB":true},"length":1},"/#R9HQ2k6cOqF_bodmAAAC":{"sockets":
{"/#R9HQ2k6cOqF_bodmAAAC":true},"length":1},"QXVLSLE":{"sockets":
{"QXVLSLE":true},"length":1}}
From here forward Im going to shorten the previous log to the relevant information at the end:
"QXVLSLE":{"sockets":
{"QXVLSLE":true},"length":1}}
And next we have the handler for the socket event where a second client wishes to join the session/room:
debug.log('Client Joining Session: ' + sessionId);
sessions[sessionId].clients[client.id] = {
name : name,
id : client.id
};
debug.log(JSON.stringify(socket.sockets.adapter.rooms));
client.join(sessionId);
debug.log(JSON.stringify(socket.sockets.adapter.rooms));
And then the log, which demonstrates the client joining the room, and again the before and after socket.sockets.adapter.rooms
object, which shows that the new client successfully joins the room:
[Debug][Join Session Event Handler]: Client Joining Session: QXVLSLE
[Debug][Join Session Event Handler]: {"QXVLSLE":{"sockets":
{"QXVLSLE":true},"length":1}
[Debug][Join Session Event Handler]: {"QXVLSLE":{"sockets":
{"QXVLSLE":true,"L1FFBDU":true},"length":2}
So that seems to be working properly, however now, I would expect the following code to send a message to clients within the room, and it isn't happening:
socket.to(sessionId).emit('receive message', {
name : data.name,
message : data.message
});
For sure this code is running, I see the logs, and the session ID seems to be correct, the logs match up there too. The receive message
event is being emitted on the server side and listened for on the client side, but no message is coming through. So I'm assuming I've done something wrong with the room handling, but I don't know what it could be.
Am I doing the room creation / emission wrong? Or should I look elsewhere for problems?
Upvotes: 0
Views: 53
Reputation:
Wow, alright, I figured this out. It was indeed a problem with my code handling the room:
socket.to(sessionId).emit('receive message', {
name : data.name,
message : data.message
});
should simply be:
socket.emit('receive message', {
name : data.name,
message : data.message
});
Upvotes: 0