简体   繁体   English

Socket.io +快递房

[英]Socket.io + express rooms

I am working on an online website with real-time chat and I've been trying to set up socket.io rooms for a few days now, but one of my custom events just doesn't emit. 我正在一个实时聊天的在线网站上工作,我一直在尝试设置socket.io房间几天,但我的一个自定义事件不会发出。

My website has 2 paths, / and /play . 我的网站有2条路径, //play When someone fills out a form in the / path, the website redirects them to the /play path. 当某人填写/ path中的表单时,网站会将其重定向到/play路径。

const io = require('socket.io')(listener);

io.on('connection', socket => {

  socket.on('add user', data => { // This event gets emitted when the user submits the form at `/`, in a client-side js file. 
    socket.username = data.username;
    socket.room = data.room;
    socket.score = 0;
    socket.join(data.room);
    io.sockets.in(data.room).emit('user joined', { // This event gets 'handled', in another (not the same one) client-size js file.
    username: data.username,
    room: data.room
  });
  });

  socket.on('disconnect', function () {
   io.sockets.in(socket.room).emit('user left', {
   username: socket.username,
   room: socket.room
  });
  socket.leave(socket.room)
});


});

In the other client-side js file: 在另一个客户端js文件中:

var socket = io.connect();  
let joined = 0;

socket.on('user joined', data => {
  joined++;
  console.log(joined, data)
  const elemental = document.createElement("LI");
  const info = document.createTextNode(data.username);
     elemental.appendChild(info);
   document.getElementById('people').appendChild(info)
});

The add user event code executes just fine, but the user joined one doesn't.. I'm almost certain that it has to do with the paths. 添加用户事件代码执行得很好,但用户加入的不是..我几乎可以肯定它与路径有关。 I read on socket.io namespaces but it seems like they are just like a different type of rooms. 我读了socket.io命名空间,但看起来它们就像一个不同类型的房间。 Can someone tell me what's wrong? 有人能告诉我什么是错的吗?

EDIT: I should mention that no errors come up. 编辑:我应该提到没有错误出现。

Without seeing your whole code, I'm going to assume that the socket on client-side.js code, is not joined to the data.room room, and that's why you don't get anything on the 'user join' event listener and that happens because you probably have multiple socket.io connections on the client side code. 在没有看到你的整个代码的情况下,我将假设client-side.js代码上的套接字没有连接到data.room会议室,这就是为什么你没有在'user join'事件监听器上得到任何东西的原因这是因为您可能在客户端代码上有多个socket.io连接。

You have multiple ways to solve this, depending on what you're trying to achieve. 您有多种方法可以解决这个问题,具体取决于您要实现的目标。

  • Instead of only emitting to people on the room, emit to all sockets that an user joined a specific room. 不是仅向房间内的人发射,而是向用户加入特定房间的所有插座发射。 io.emit('user joined') instead of io.sockets.in(data.room)... io.emit('user joined')而不是io.sockets.in(data.room)...
  • There should only be one single var socket = io.connect(); 应该只有一个单独的var socket = io.connect(); on the front end, otherwise the socket that's emitting: add user and therefore joined to data.room is not the same socket that's listening on: user joined , and that's why you never get the event on that socket, because you have 2 different sockets, one that joined the room, and one that is doing absolutely nothing except waiting for an event that will never occur. 在前端,否则是发出的套接字: add user并因此加入到data.room不是正在监听的套接字: user joined ,这就是为什么你永远不会在该套接字上获取事件,因为你有2个不同的套接字,一个加入房间的人,除了等待永远不会发生的事件之外什么都没做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM