简体   繁体   English

如何获取参数值

[英]How to get the value of parameter

How can I get the value of my parameter to the socket.io server? 如何将参数的值获取到socket.io服务器?

In my client I have this simple code: 在我的客户中,我有以下简单代码:

var id = '0001';
var userame = 'Shane';

socket.on('news', function (data) {
    console.log(data);
    socket.emit('getdetails', {id:id},{username:username});
});

In the server (app.js): 在服务器(app.js)中:

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('getdetails', function (data) {
        console.log(data.username);
    });
});

But username is undefined. 但是用户名未定义。 How to get the value of my username? 如何获取我的用户名的值?

I think you need to add user name and id to the data param in the client. 我认为您需要在客户端的数据参数中添加用户名和ID。 It looks like when server gets it, data is empty. 看起来当服务器获取它时,数​​据为空。

Try {id:id,username:username} 尝试{id:id,username:username}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM