我在服务器上有以下代码:
var game = io.listen(app);
game.sockets.on('connection', function(socket){
storePlayers(socket.id);
//Only the player the connects gets this message
socket.emit('firstTime', {message: 'Welcome!'});
//If a player disconnects, a message will be sent to all the sockets.
socket.on('disconnect', function(){
game.sockets.emit('exit', {message: 'Player ' + socket.id + ' left the game...!'});
});
//When a player connects, all the players will get this message.
game.sockets.emit('entrance', {message: 'Player ' + socket.id + ' is online'});
});
我想掌握这个storePlayers(id)
功能。这样做的原因是当套接字连接时,应该socket.id
在客户端使用特定对象创建一个对象。
我将如何去做这样的事情?