无法向特定房间广播消息? TypeError:无法在'中调用方法'未定义的

时间:2016-07-15 13:29:26

标签: node.js socket.io

我对socket.io很新。我尝试使用socket.io在客户端和服务器之间发送消息。我希望服务器将消息广播到特定的房间,但我没有成功。错误是" socket上缺少错误处理程序。 TypeError:无法在'中调用方法'未定义的"。我尝试简化我的代码,以便只输入房间ID,如下面的代码, 123 的房间,但我仍然有同样的错误。

服务器端代码:

//For checking the username and display registered user
client.on("join",function(name){
    clients.push(client); 
    console.log("Join Event Successfully");
});

//Create Room on server side
client.on("createRoom", function(name) {
    console.log(name);
    client.join(name); 
    console.log("Room Create Successfully");
    console.log("Room "+ name);
    console.log("Room create successfully");
});

//Join room event

client.on("joinRoom",function(room_id){
    client.join(room_id); 
    console.log("Room Join Successfully");
});

//Socket uses to sync video in real time
//On playing event from one client and need to brocast inside the room
client.on("onPlay",function(data){
    io.to('123').emit("onPlay_server",data);
    console.log("onPlay event trigger");
});

//Socket uses to sync video in real time
//On Pause event from one client and need to brocast inside the room
client.on("onPuase",function(data){
    io.to('123').emit("onPuase_server",data);
    console.log("onPause event trigger");
});
// Event trigger when client disconnect from the sever
client.on("disconnect", function() {  
        console.log("There is someone left the room"+ toString(client.id));
});

客户端代码:

socketIO.on("onPause_server", function(currentTime){
            console.log("onPause_server");
            if(player_state === 'play'){
              console.log("onPause_server");
              player.pauseVideo();
              Player.seektTo(currentTime);
              player_state = 'pause'
            }
            Player.seektTo(currentTime);
          });
          //Get message from sever side on play message
socketIO.on("onPlay_server", function(currentTime){
            console.log("onPlay_server event trigger");
            if(player_state === 'pause'){
              console.log("onPlay_server event trigger");
              player.seekTo(currentTime);
              Player.playVideo();
              Player_state = "play";
            }
            player.seekTo(currentTime);
          });
function onPlayerStateChange(event) {

              //console.log(player.getCurrentTime());
              if (event.data == YT.PlayerState.PAUSED) {

                  player.pauseVideo();
                  var currentTime = player.getCurrentTime();
                  player.seekTo(currentTime);   
                  socketIO.emit("onPause",currentTime);
                  player_state = "paused";
              }

              else if (event.data == YT.PlayerState.PLAYING) {
                  player.playVideo();
                  var currentTime = player.getCurrentTime();
                  socketIO.emit("onPlay",currentTime);
                  player_state = "play";

              }
          }

代码运行正常,直到客户端尝试发送消息" onPlay"或" onPause"到服务器端,服务器立即产生错误" socket上缺少错误处理程序。 TypeError:无法在'中调用方法'未定义的"

0 个答案:

没有答案