在套接字连接上发送附加数据
如何在套接字连接时最好地发送附加数据?
客户端:
socket.on('connect',function(){
//I'd like set some values and pass them up to the node server.
});
Node.js 服务器:
io.on('connection', function(client){
//I'd like to get the data here.
});
例如发送用户名或电子邮件地址等。
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:
io.on('connection', function(client){
//I'd like to get the data here.
});
For example sending a user name or email address etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该在连接或创建时发送数据:
使用新版本的socket.io ,服务器上的内容已更改:
You should send your data either in connect or on create:
With the new version of socket.io, on server is where the things have been changed:
我有一种不同的方法 - 在连接后立即使用数据发出一个事件:
您可以保存一个变量/对象,并且说,当没有带有数据的 ehlo 事件时,其他事件将被忽略,直到
ehlo
已发送。如果这不满足,您可以在连接时立即发送数据,我不会复制代码,但它在这个答案中: https: //stackoverflow.com/a/13940399/1948292
I have a different approach - emit an event right after connecting, with the data:
You can hold a variable/object, and say, when there is no
ehlo
event with data, the other events are ignored untilehlo
is sent.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
一旦 TCP 连接建立,
connection
事件就会被触发。中间没有办法发送任何东西。您可以做的就是简单地获取服务器发送的第一条消息并将数据放入该消息中。我强烈建议为此推出一些瘦协议,这样您将拥有多种类型的消息,并使用它们来确定代码应如何处理数据。
这也将更具可扩展性,因为为此提出通用架构相当容易。如果您想先等待
userdata
进入,您只需向连接添加一些状态即可。The
connection
events get fired as soon as the TCP connection is established. There's no way to send anything in between.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
userdata
to come in first you can simply add some state to your connections.这是我在套接字版本 2.1.1 上的代码
this is my code on socket version 2.1.1
这是我用于将查询数据发送到nodejs和server.io服务器客户端
查询的代码...user1
this my code for sending query data to nodejs and server.io server client
query...user1
我发现此处的答案在这方面非常有帮助: 发送自定义数据以及socket.io中的handshakeData?
另外,复习以下文档有助于解释将变量绑定到握手对象的方法,该对象可以在连接期间从套接字对象访问:https://github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization
I found the answer located here really helpful in this regard: Send custom data along with handshakeData in 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
我假设您将使用 SSL。
使用查询字符串发送敏感数据并不是一个好的安全实践 (为什么?)。
我认为,如果您需要发送非敏感数据,那么 Miquel 答案是最快且更简单的答案,但如果您需要发送敏感数据,那么像 Daniel W. 这样的方法是最好的方法。
对于身份验证方法,您可以查看此 github 项目 (socketio-auth),其中除了目前没有维护这一事实之外,还可以让您了解如何处理问题。
I'm assuming you will use 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.
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.