node.js将userid客户端发送到服务器

时间:2015-07-12 21:14:03

标签: node.js sockets socket.io

我无法将客户端costum userid发送到node.js

由于某种原因,它不会将用户标识发送到服务器以提交它。节点日志中没有输出。

客户支持:

var socket = io.connect('http://sitename.eu:1337/');

var user_id = <?echo $data->userid?>;


socket.on('connection',function(user_id) {
    socket.emit('setuserid', user_id);

});

服务器支持:

io.on('connection', function (socket){ // just to prove that the code is inside it

logger.info('SocketIO > Connected socket ' + socket.id);


socket.on('setuserid', function (userId) {
    users[userId]=socket;
    socket.userid = userId;
    logger.info("setting userid...");
});

仅输出:

2015-07-12T21:09:05.656Z - info: SocketIO > Connected socket E2ghiXGfTJWtgWs4AAAA
2015-07-12T21:09:08.134Z - info: SocketIO > Connected socket XY0c-A_ZveEUs_CuAAAB
2015-07-12T21:09:21.393Z - info: SocketIO > Disconnected socket E2ghiXGfTJWtgWs4AAAA
2015-07-12T21:09:22.610Z - info: SocketIO > Connected socket G3DmRKSymCuaximOAAAC

1 个答案:

答案 0 :(得分:0)

更改此行:

socket.on('connection',function(user_id) {

成:

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

Socket.io触发一个名为connecthttp://socket.io/docs/client-api/#io#emitter)的事件。由于永远不会调用connection,因此该函数永远不会运行。

此外,请务必删除user_id作为参数。

否则,您将覆盖全局范围,并将作为userid的结果发送undefined

相关问题