node.js 在套接字连接上发送附加数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4743592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send additional data on socket connection
提问by Dan
How to best send additional data upon socket connection?
如何在套接字连接时最好地发送附加数据?
Client:
客户:
socket.on('connect',function(){
//I'd like set some values and pass them up to the node server.
});
Node.js Server:
Node.js 服务器:
io.on('connection', function(client){
//I'd like to get the data here.
});
For example sending a user name or email address etc.
例如发送用户名或电子邮件地址等。
回答by Miquel
You should send your data either in connect or on create:
您应该在连接或创建时发送数据:
var s = io('http://216.157.91.131:8080/', { query: "foo=bar" });
s.connect();
var c = io.connect('http://216.157.91.131:8080/', { query: "foo=bar" });
With the new version of socket.io, on server is where the things have been changed:
使用新版本的 socket.io,服务器上的事情发生了变化:
var io = require('socket.io')(server);
io.use(function(socket, next) {
var handshakeData = socket.request;
console.log("middleware:", handshakeData._query['foo']);
next();
});
回答by Daniel W.
I have a different approach - emit an event right after connecting, with the data:
我有一个不同的方法 - 连接后立即发出一个事件,数据:
socket.on('connect',function(){
// Send ehlo event right after connect:
socket.emit('ehlo', data);
});
io.on('connection', function(client){
// Receive ehlo event with data:
client.on('ehlo', function(data) {
});
});
You can hold a variable/object, and say, when there is no ehloevent with data, the other events are ignored until ehlois sent.
您可以持有一个变量/对象,并且说,当没有ehlo带有数据的事件时,其他事件将被忽略,直到ehlo被发送。
If this does not satisfy, you can send data right when connecting, I won't copy the code but it is in this answer: https://stackoverflow.com/a/13940399/1948292
如果这不满足,你可以在连接时发送数据,我不会复制代码,但它在这个答案中:https: //stackoverflow.com/a/13940399/1948292
回答by Ivo Wetzel
The connectionevents get fired as soon as the TCP connection is established. There's no way to send anything in between.
connection一旦建立 TCP 连接,事件就会被触发。没有办法在两者之间发送任何东西。
What you can do is to simply take the first message send by the server and put the data in that message. I'd strongly suggest to roll some thin protocol for this, so you would have multiple types of messages and use those to determine how the code should process the data.
您可以做的是简单地获取服务器发送的第一条消息并将数据放入该消息中。我强烈建议为此推出一些瘦协议,因此您将拥有多种类型的消息并使用这些消息来确定代码应如何处理数据。
This would be more extendable too, since it's fairly easy to come up with a generic architecture for that. If you want to wait for the userdatato come in first you can simply add some state to your connections.
这也将更具可扩展性,因为为此提出通用架构相当容易。如果您想等待userdata进入,您可以简单地向您的连接添加一些状态。
回答by mukut
this my code for sending query data to nodejs and server.io server client
这是我将查询数据发送到 nodejs 和 server.io 服务器客户端的代码
var socket = io.connect(window.location.origin,{query:'loggeduser=user1'});
io.sockets.on('connection', function (socket) {
var endp = socket.manager.handshaken[socket.id].address;
console.log("query... " + socket.manager.handshaken[socket.id].query.user);
});
query...user1
查询...user1
回答by Art Geigel
I found the answer located here really helpful in this regard: Send custom data along with handshakeData in socket.io?
我发现这里的答案在这方面真的很有帮助:在 socket.io 中发送自定义数据和握手数据?
Also brushing up on the following documentation helps explain ways to tie variables to the handshake object which can be accessed from the socket object during the connection: https://github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization
同时复习以下文档有助于解释将变量绑定到握手对象的方法,该对象可以在连接期间从套接字对象访问:https: //github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization
回答by Manoj Rana
this is my code on socket version 2.1.1
这是我在套接字版本 2.1.1 上的代码
//client side
var socket = io.connect(window.location.origin,{query:'loggeduser=user1'});
// server side
io.socket.on('connection', function (socket) {
console.log("loggeduser => " + socket.handshake.query.loggeduser);
});
回答by oriuken
I'm assuming you will use SSL.
我假设您将使用 SSL。
Using querystrings to send sensitive data is not a good security practice (why?). I think that if you need to send non-sensitive data, then Miquel answer is the fastest and more easy one, but if sensitive data is what you need to send, then something like Daniel W. is the best approach to take.
使用查询字符串发送敏感数据不是一个好的安全实践(为什么?)。我认为,如果您需要发送非敏感数据,那么 Miquel 的回答是最快、更简单的答案,但如果您需要发送敏感数据,那么像 Daniel W. 这样的方法是最好的方法。
For an authentication approach, you can take a look at this github project (socketio-auth) which, beyond the fact that it is currently with no maintenance, can give you a good idea of how to deal with the problem.
对于身份验证方法,您可以查看这个 github 项目(socketio-auth),除了目前没有维护的事实之外,它可以让您很好地了解如何处理问题。

