如何获取向nodejs服务器发送数据(.emit())的用户的详细信息? [英] How to get details of the user that sent data (.emit()) to nodejs server?
问题描述
我正在尝试使用nodejs
,express
和socket.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屋!