Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
io.sockets.adapter.rooms[roomId] returns undefined after using socket.join[roomId]?

I've been trying to create a sort of multiplayer game where rooms are made by users in socket.io, but joining a user to a room has been difficult, and I recently realized it seems that the rooms aren't actually being created? I know for a fact the "handleNewRoom" function is being run as it does generate an ID and send it to the frontend, so I know "socket.join(roomId);" is being run as well, but even in the same function "io.sockets.adapter.rooms[roomId];" returns undefined (this line is purely there for testing, I meant to only call it in the handleJoinRoom function to see if the room exists at all but I moved it here so it'd be faster to check the existence)

I've been messing with socket.io for a little bit on this project so there might be something very simple that I'm missing, so here's my full server.js file. As far as I've read, the socket.join[roomId] command should create a room regardless of its existence, and then the io.sockets.adapter.rooms[roomId] should return a Map of the specified room, but for some reason it doesn't. Any ideas?

const express = require("express");
const app = express();
const path = require("path");
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

// pull the function that randomly generates an ID
const { makeid } = require("./utils");

// set a variable with all the rooms
const socketRooms = {};

// set the path for the frontend
app.use(express.static(path.join(__dirname, "../front")));

const PORT = process.env.PORT || 3000;

// start the server
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));

// on a new connection to the main server
io.on("connection", (socket) => {
  console.log("User has connected");
  socket.on("newRoom", handleNewRoom);
  socket.on("joinRoom", handleJoinRoom);
  let users = [];

  function handleNewRoom() {
    let roomId = makeid(5);
    socket.join(roomId);
    socketRooms[socket.id] = roomId;
    socket.emit("roomId", roomId);
    socket.number = 1;
    socket.emit("init", 1);
    const room = io.sockets.adapter.rooms[roomId];
    if (room) {
      console.log("I definitely exist");
    }
  }

  function handleJoinRoom(roomId) {
    const room = io.sockets.adapter.rooms[roomId];
    let allUsers;
    if (room) {
      allUsers = room.sockets;
      console.log("I'm here");
    }
    console.log(allUsers);
    console.log(roomId);
    let numSockets = 0;
    if (allUsers) {
      numSockets = Object.keys(allUsers).length;
    }
    if (numSockets === 0) {
      socket.emit("unknownRoom");
      return;
    }
    socketRooms[socket.id] = roomId;
    socket.join(roomId);
    socket.number = 2;
    socket.emit("init", 2);
    socket.emit("roomId", roomId);
  }
});

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda