我正在使用NodeJs v0.10.28,每次我尝试登录聊天时都会收到错误消息
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;
});
Run Code Online (Sandbox Code Playgroud)
和
socket.set('nickname', name, function () {
// this kind of emit will send to all! :D
io.sockets.emit('chat', {
msg : "Welcome, " + name + '!',
msgr : "Nickname"
});
});
Run Code Online (Sandbox Code Playgroud)
完整的代码
您必须直接在套接字中设置nickname属性!
来自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
});
});
Run Code Online (Sandbox Code Playgroud)
请查看以下示例:https://github.com/Automattic/socket.io/blob/master/examples/chat/index.js
| 归档时间: |
|
| 查看次数: |
8838 次 |
| 最近记录: |