I Nachricht an bestimmten Raum senden möchten (alle Kunden im Raum), habe ich viel gesucht und versucht, diese Optionen:Nachricht kann nicht an bestimmten Raum senden [socket.io]
io.in(socket.RoomId).emit('Test');
io.to(socket.RoomId).emit('Test');
io.sockets.in(socket.RoomId).emit('Test');
Ich wollte als Server zu senden, also ohne einen vorhandenen Socket im Raum zu benutzen. Aber weil ich nicht einen Weg finden konnte habe ich versucht, die folgenden:
this.ListOfSockets[0].to(RoomId).emit('Test');
Das funktionierte nicht, so habe ich versucht, nur an die Steckdose emittieren sich wie folgt aus:
this.ListOfSockets[0].emit('Test');
Und das war als ich erwartet.
Was mache ich falsch? Warum kann ich nicht als Server an alle Clients in einem bestimmten Raum senden?
Update 2: etwas mehr Code für li X
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');
});
Vielen Dank im Voraus Hinzufügen
Wenn Sie 'OpenRoom [OpenRoom.length - 1] .RID' protokollieren, was passiert an jedem Punkt? –
'socket.join (OpenRoom [OpenRoom.length - 1] .RID);' Auch sollte das nicht 'socket.join (OpenRoom [OpenRoom [0]]. RID) sein;' Wenn du es nicht hinzufügst? –
Ja nachdem du es erwähnt hast habe ich es auf 0 geändert. Bei Los bekomme ich auf jeder Stufe die gleiche ID, also muss das gut funktionieren. Kann es auf Kundenseite falsch sein? Dies ist mein Code auf der Clientseite: socket.On ("Test", onTest); (ist C# -Code für Unity Game Engine) –