I'm building a small game in React with Socket.io and Node. But I encountered an issue where the client does not recieve any messages sent to the room. I use the following code in the Node.js server:
Server
app.js
io.on("connection", (socket) => {
initGame(io, socket);
});
game.js
exports.initGame = function (sio, socket) {
socket.on("joinGameRoom", joinGameRoom);
socket.on("logSocket", logSocket);
};
function logSocket() {
let sock = this;
console.log(sock.rooms);
}
async function joinGameRoom(data) {
let sock = this;
let game = data.game;
await sock.join(String(game._id));
await sock.in(String(game._id)).emit("playerJoined", game);
await sock.emit("game", game);
}
Client
game.js
socket.on("playerJoined", (data) => {
setGame(() => data);
console.log("player joined!");
});
The listener in the client/game.js is never called. And when I call LogSocket, the only room the socket is in; is it's own room. But other calls have been getting called; calls like "game" :
Client
game.js
socket.on("game", (data) => {
setGame(() => data);
console.log("game Call");
});
So my question is: what is it that I'm doing wrong here? Why did my socket leave the room on it's own?