In my project https://glitch.com/~abroad-hollow-knight I have the following lines of code:
io.on("connection", socket => {
socket.on("room", roomname => {
console.log("room: " + roomname);
socket.room = roomname;
socket.join(socket.room)
});
});
io.on("connection", socket => {
socket.on("column", column => {
console.log(socket.room)
console.log("column: " + column);
io.to(socket.room).emit("column", column)
});
});
in the first part, console.log(socket.room) gives the room I have put in somewhere else in the code, but in the second part, the console.log(socket.room) gives undefined. What have I done wrong?
I’ve changed this to #coding-help and added the #websocket tag.
1 Like
io.on("connection", socket => {
socket.on("room", roomname => {
console.log("room: " + roomname);
socket.room = roomname;
socket.join(socket.room)
});
});
//add this:
console.log(socket.room)
io.on("connection", socket => {
socket.on("column", column => {
console.log(socket.room)
console.log("column: " + column);
io.to(socket.room).emit("column", column)
});
});
The variable might not be accessible in the next function.
This did not work. When I added console.log(socket.room) in it gives an error saying ‘socket is not defined.’
That must be your problem. Did you try re-installing sockets?
system
Closed
April 22, 2021, 8:50pm
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.