0

服务器.js

socket.on('adduser', username, successcallback){

if(users.hasOwnProperty(username)){

} else {

    socket.username = username;

    users[username] = { name: username };

    socket.emit('updatechat', 'SERVER', 'you have connected');

    socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');

    io.sockets.emit('updateusers', users);

    successcallback({ msg: 'User ' + username + ' has been successfully added' });
}

});

索引.html

socket.on('connect', function(){

    var username = prompt('Whats your name?');

    socket.emit('adduser', username, function(data){
        console.log(data);
    });

});

这使我的应用程序崩溃。这不是应该如何实现回调吗?

4

1 回答 1

1

adduser 消息处理程序的定义缺少function(...). 它应该是:

socket.on('adduser', function(username, successcallback) {
  ...
});
于 2012-09-23T14:14:28.240 回答