简体   繁体   中英

unable to emit messages to rooms in socket.io

whenever user connects it looks for a room which has people less than its maxm no and joins it,if not available it creates its own room. The room creation code is perfect and i am not mentioning it top avoid complexity. The code which i am mentioning is not executing :

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;
  });

Using this code to send a message to the room:

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

Referent: https://socket.io/docs/emit-cheatsheet/#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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