在套接字连接上发送附加数据 [英] Send additional data on socket connection
本文介绍了在套接字连接上发送附加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在套接字连接时最好地发送附加数据?
How to best send additional data upon socket connection?
客户:
socket.on('connect',function(){
//I'd like set some values and pass them up to the node server.
});
Node.js 服务器:
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.
推荐答案
您应该在连接或创建时发送数据:
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" });
随着 socket.io 的新版本,在服务器上事情已经改变了:
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();
});
这篇关于在套接字连接上发送附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文