Reputation: 103
I want to send message to specific room (all clients in room), I have searched a lot and tried these options:
io.in(socket.RoomId).emit('Test');
io.to(socket.RoomId).emit('Test');
io.sockets.in(socket.RoomId).emit('Test');
I wanted to send as a server, so without using an existing socket in the room. But because I could not find a way I tried the following:
this.ListOfSockets[0].to(RoomId).emit('Test');
This worked not, so I tried to just emit to the socket itself like this:
this.ListOfSockets[0].emit('Test');
And this was working as I expected.
What am I doing wrong? Why can't I send as a server to all the clients in a specific room?
Update 2: Adding some more code for 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');
});
Thanks in advance
Upvotes: 1
Views: 1875
Reputation: 4051
By default all sockets are joined to there own personal room when the socket.id is derived but to create sepereate rooms you'll need to call socket.join('foo');
on the socket's which you wish to become part of a room.
Then after which you'll be able to send message's to the room using io.to('foo').emit('test');
Also note that you can also add broadcast
between io.
and .to
which will allow you to send a message to all sockets except the socket who made the call.
socket.join(OpenRoom[OpenRoom.length - 1].RID);
Also this should be
socket.join(OpenRoom[OpenRoom[0]].RID);
You'll need to use socket.On('Test', data =>{ //code here})
Within this you can make the call to your function onTest and pass the necessary information via data.
Upvotes: 2