Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t向特定房间发送消息[socket.io]_Javascript_Node.js_Socket.io - Fatal编程技术网

Javascript Can';t向特定房间发送消息[socket.io]

Javascript Can';t向特定房间发送消息[socket.io],javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我想将消息发送到特定房间(房间中的所有客户),我搜索了很多,并尝试了以下选项: io.in(socket.RoomId).emit('Test'); io.to(socket.RoomId).emit('Test'); io.sockets.in(socket.RoomId).emit('Test'); 我想作为服务器发送,因此不需要使用房间中现有的插座。但由于找不到方法,我尝试了以下方法: this.ListOfSockets[

我想将消息发送到特定房间(房间中的所有客户),我搜索了很多,并尝试了以下选项:

        io.in(socket.RoomId).emit('Test');
        io.to(socket.RoomId).emit('Test');        
        io.sockets.in(socket.RoomId).emit('Test');
我想作为服务器发送,因此不需要使用房间中现有的插座。但由于找不到方法,我尝试了以下方法:

this.ListOfSockets[0].to(RoomId).emit('Test');
这不起作用,所以我尝试像这样发射到套接字本身:

this.ListOfSockets[0].emit('Test');
正如我所预料的那样

我做错了什么?为什么我不能作为服务器发送到特定房间中的所有客户端

更新2: 为lix添加更多代码

io.on('connection', function(socket){

console.log('client connected');
playerCount++;

//For testing purposes I give player random generated string using: var shortid = require('shortid');
socket.id = shortid.generate();
console.log('id of this socket is:' + socket.id);

//When client decides to play the game and want's to join a random room
socket.on('JoinRoom', function () 
{
    //Checks if there exist an open room
    if(OpenRoom.length > 0)
    {
        //Joins first room in openroom list
        socket.join(OpenRoom[0].RID);
        socket.RoomId = OpenRoom[0].RID;

        //Joinedroom lets player open the next scene in my game
        socket.emit('JoinedRoom');

        //Joinedroom is a function that sends information to the socket about the room and the state of the game,
        //adding the the player to room etc.
        JoinedRoom(socket.id, OpenRoom[0].RID, socket);
    }
    else
    {
        //If there is no open room, it creates a new room with new id.
        OpenRoom.push({ RID:shortid.generate(), TimeOutID: null });
        //Joins room
        socket.join(OpenRoom[OpenRoom.length - 1].RID);
        socket.RoomId = OpenRoom[OpenRoom.length - 1].RID;

        //Creates room and fills information to it
        socket.emit('JoinedRoom');
        var NewRoom = new Room(OpenRoom[OpenRoom.length - 1].RID)
        NewRoom.addPlayer(socket.id);
        NewRoom.addSocket(socket);

        Rooms.push(NewRoom);

        OpenRoom[OpenRoom.length - 1].TimeOutID = setTimeout(CloseRoom, 5000, OpenRoom[OpenRoom.length - 1]);
    }

    //Checking if RoomId exists, it exists.
    console.log(socket.RoomId);

    //When I send 'test' to client, the client should print 'Succesfully Tested' to the console

    //For io.to I dont get the message 'Succesfully Tested'
    io.to(socket.RoomId).emit('Test');

    //With socket.emit I get 'Succesfully Tested' message inside my console at the client side.
    socket.emit('Test');

});

提前感谢

默认情况下,在派生socket.id时,所有套接字都会连接到自己的个人房间,但要创建单独的房间,您需要调用
socket.join('foo')

然后,您可以使用
io.to('foo').emit('test')将消息发送到房间
还请注意,您还可以在
io.
之间添加
广播
,这将允许您向所有套接字发送消息,但发出呼叫的套接字除外

socket.join(OpenRoom[OpenRoom.length-1].RID)
这也应该是
socket.join(OpenRoom[OpenRoom[0]].RID)


您需要使用
socket.On('Test',data=>{//code here})
在这个函数中,您可以调用您的函数onTest并通过data传递必要的信息。

你好,谢谢您的解释,但我已经加入了一个单独的房间。这是代码:socket.join(OpenRoom[OpenRoom.length-1].RID);socket.RoomId=OpenRoom[OpenRoom.length-1].RID;我将房间id添加到socket.RoomId,并使用该房间id向房间发送消息,但没有成功。如果您可以将其编辑到您的问题中,我将在您将其添加为代码后进行一些测试:-)谢谢@lix,我的问题没有解决,但我认为问题出在我用于unity游戏引擎的插件上,我打算和他联系解决这个问题。谢谢你的指导和帮助!如果您记录
OpenRoom[OpenRoom.length-1].RID
每个点发生了什么?
socket.join(OpenRoom[OpenRoom.length-1].RID)
也不应该是
socket.join(OpenRoom[OpenRoom[0]].RID)如果您没有添加到它?是的,在您提到它之后,我已将其更改为0。在很多时候,我在每个阶段都得到了相同的ID,所以这一定很有效。在客户端可能是错误的吗?这是我在客户端的代码:socket.On(“Test”,onTest);(是unity游戏引擎的C#代码)你需要使用
socket。在('Test',data=>{//code here})
中,你可以调用你的函数
onTest
,并通过数据传递必要的信息。@Saut Alkan我希望我的答案对你有所帮助,如果它仍然不起作用,请告诉我!