socket.get()和socket.set()给出了问题

我正在使用NodeJs v0.10.28,每次我尝试login聊天,我得到一个错误

TypeError: Object #<Socket> has no method 'set' at Socket.<anonymous>

如果我删除线的作品,但不正确的工作。

这段代码中的错误是什么?

 // get the name of the sender socket.get('nickname', function (err, name) { console.log('Chat message by ', name); console.log('error ', err); sender = name; }); 

 socket.set('nickname', name, function () { // this kind of emit will send to all! :D io.sockets.emit('chat', { msg : "Welcome, " + name + '!', msgr : "Nickname" }); }); 

完整的代码

http://pastebin.com/vJx7MYfE

你必须直接在套接字中设置昵称属性!

从socket.io网站:

旧的io.set()和io.get()方法已被弃用,仅支持向后兼容。 这是一个旧的授权示例到中间件风格的翻译。

另外,从一个socket.io网站的例子:

 // usernames which are currently connected to the chat var usernames = {}; var numUsers = 0; // when the client emits 'add user', this listens and executes socket.on('add user', function (username) { // we store the username in the socket session for this client socket.username = username; // add the client's username to the global list usernames[username] = username; ++numUsers; addedUser = true; socket.emit('login', { numUsers: numUsers }); // echo globally (all clients) that a person has connected socket.broadcast.emit('user joined', { username: socket.username, numUsers: numUsers }); }); 

检查这里的例子: https : //github.com/Automattic/socket.io/blob/master/examples/chat/index.js

就像Ludo所说的那样,直接在套接字中设置昵称属性:

 // get the name of the sender var name = socket.nickname; console.log('Chat message by ', name); console.log('error ', err); sender = name; 

 // this kind of emit will send to all! :D socket.nickname = name; io.sockets.emit('chat', { msg : "Welcome, " + name + '!', msgr : "Nickname" });