socket.io上的方法回调

时间:2012-09-23 13:52:09

标签: javascript html5 node.js websocket socket.io

server.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' });
}

});

的index.html

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

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

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

});

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

1 个答案:

答案 0 :(得分:1)

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

socket.on('adduser', function(username, successcallback) {
  ...
});