0

I am trying to solve the following problem: People can connect to a room and every 5 seconds 2 people are randomly selected from this room to start a game (if at least 2 people are in the room). The game is a new room.

So I am joining the people to waiting room with io['client'].join('waitingRoom'); and saving his ID io['client']['id'] to the database (So basically I save a string like 'Dde7Wp41kf0EA3r3AAAA' in the database).

I have a task which is running every 5 seconds which randomly selects 2 IDs from the database and the problem is that I do not know how to add these 2 IDs in the new 'game' room. I have only a hash (some string) and not a socket object. Can I do this?

4

1 回答 1

1

You should use the connected object of io.sockets. It stores sockets, you can retrieve them by id.

http://socket.io/docs/server-api/#namespace#connected:object

Example:

io.sockets.connected[socketIoID].emit('message', {data: 'test'});
于 2014-07-13T21:03:59.203 回答