在套接字连接上发送附加数据

发布于 2024-10-13 02:43:10 字数 314 浏览 2 评论 0原文

如何在套接字连接时最好地发送附加数据?

客户端:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凡尘雨 2024-10-20 02:43:10

您应该在连接或创建时发送数据:

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" });

使用新版本的socket.io ,服务器上的内容已更改:

var io = require('socket.io')(server);

io.use(function(socket, next) {
  var handshakeData = socket.request;
  console.log("middleware:", handshakeData._query['foo']);
  next();
});

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:

var io = require('socket.io')(server);

io.use(function(socket, next) {
  var handshakeData = socket.request;
  console.log("middleware:", handshakeData._query['foo']);
  next();
});
蓝海似她心 2024-10-20 02:43:10

我有一种不同的方法 - 在连接后立即使用数据发出一个事件:

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) {
    });
});

您可以保存一个变量/对象,并且说,当没有带有数据的 ehlo 事件时,其他事件将被忽略,直到ehlo 已发送。

如果这不满足,您可以在连接时立即发送数据,我不会复制代码,但它在这个答案中: https: //stackoverflow.com/a/13940399/1948292

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 ehlo event with data, the other events are ignored until ehlo 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

阳光的暖冬 2024-10-20 02:43:10

一旦 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.

阳光的暖冬 2024-10-20 02:43:10

这是我在套接字版本 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);
});

this is my code on socket version 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);
});
蓝颜夕 2024-10-20 02:43:10

这是我用于将查询数据发送到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);
});

查询的代码...user1

this my code for sending query data to nodejs and server.io server client

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

颜漓半夏 2024-10-20 02:43:10

我发现此处的答案在这方面非常有帮助: 发送自定义数据以及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

安静 2024-10-20 02:43:10

我假设您将使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文