簡體   English   中英

無法向socket.io中的房間發送消息

[英]unable to emit messages to rooms in socket.io

每當用戶連接時,它會尋找房間的人數少於其最大人數的房間並加入該房間,如果不可用,它將創建自己的房間。 房間創建代碼是完美的,為了避免復雜性,我沒有在上面提到它。 我提到的代碼未執行:

server/server.js
if((player_lst.length) === 2){
    console.log(player_lst.length);
this.to(room_id).emit('quiz', {
    Question:questions
});}
else{
    console.log(player_lst.length);
    this.to(room_id).emit('waiting', {
       text:'waiting'
    });
}   

main.js

 socket.on('connect',function(){
  socket.emit('new_player',{
      id:socket.id
  });
  console.log('bnnn');
});
socket.on('quiz',gameplay);
  socket.on('waiting',function(data){
      console.log('bcbcb');
  var p= document.getElementById('message');
    p.innerHTML= data.text;
  });

使用以下代碼向房間發送消息:

// sending to all clients in 'game' room except sender
socket.to('game').emit('nice game', "let's play a game");

引用對象: https : //socket.io/docs/emit-cheatsheet/#

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM