Node.js设置套接字ID

从Node.js的官方聊天示例开始
提示用户通过“注册”(client.html)将他的名字发送到服务器:

while (name == '') { name = prompt("What's your name?",""); } socket.emit('register', name ); 

服务器收到名称。 我想让它作为套接字的标识符的名字。 所以,当我需要发送一个消息给该用户,我发送到他的名字(姓名存储在数据库中的信息)的套接字。
更改将在这里发生(server.js):

  socket.on('register', function (name) { socket.set('nickname', name, function () { // this kind of emit will send to all! :D io.sockets.emit('chat', { msg : "naay nag apil2! si " + name + '!', msgr : "mr. server" }); }); }); 

我正在努力做这个工作,因为如果我不能识别sockets,我不能走得更远。 所以任何帮助将非常感激。
更新:我明白,昵称是一个参数的套接字,所以问题是更具体的:如何获得具有“凯尔”作为昵称发出消息的套接字?

将您的套接字存储在这样的结构中:

 var allSockets = { // A storage object to hold the sockets sockets: {}, // Adds a socket to the storage object so it can be located by name addSocket: function(socket, name) { this.sockets[name] = socket; }, // Removes a socket from the storage object based on its name removeSocket: function(name) { if (this.sockets[name] !== undefined) { this.sockets[name] = null; delete this.sockets[name]; } }, // Returns a socket from the storage object based on its name // Throws an exception if the name is not valid getSocketByName: function(name) { if (this.sockets[name] !== undefined) { return this.sockets[name]; } else { throw new Error("A socket with the name '"+name+"' does not exist"); } } };