如何获取向nodejs服务器发送数据(.emit())的用户的详细信息? [英] How to get details of the user that sent data (.emit()) to nodejs server?

查看:244
本文介绍了如何获取向nodejs服务器发送数据(.emit())的用户的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nodejsexpresssocket.io

服务器端:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

    socket.on('send_stuff', function(stuff){

        io.emit('log_stuff',newStuff);

    });

});

http.listen(54123, function(){
    console.log('listening on *:54123');
});

客户端:

var socket = io.connect('http://example.com:12345');
socket.emit('send_stuff',stuff);


我的问题是如何获取执行socket.emit('send_stuff',stuff)的客户端(ip,用户代理等)的详细信息?


My question is How do I get the details of the client (ip,user-agent,etc.) that executed socket.emit('send_stuff',stuff)?

我想将其传递给newStuff变量.

socket.on('send_stuff', function(stuff){});仅返回客户端emit()发送的值.

socket.on('send_stuff', function(stuff){}); in this line stuff only returns the value that was send by client emit().

任何想法如何做到这一点?

Any ideas how to do this?

推荐答案

您可以在connection事件中获取客户端的ip和用户代理.

You can get client's ip and user-agent in connection event.

io.on('connection', function(socket){
  console.log("ip: "+socket.request.connection.remoteAddress);
  console.log("user-agent: "+socket.request.headers['user-agent']);
})

文档可以为您提供一些提示 http://socket.io/docs/server-api/

Documentation can give you some hints http://socket.io/docs/server-api/

这篇关于如何获取向nodejs服务器发送数据(.emit())的用户的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆