Node.js 如何使用NodeJS从客户端获取套接字的headers请求
我对这件事很陌生。我只是想知道是否有可能获取web套接字客户端请求头。 我在服务器端node.js中使用:Node.js 如何使用NodeJS从客户端获取套接字的headers请求,node.js,sockets,Node.js,Sockets,我对这件事很陌生。我只是想知道是否有可能获取web套接字客户端请求头。 我在服务器端node.js中使用: 使用ExpresJS,我可以获得如下标题: router.post('/', function (req, res, next) { console.log(req.headers); })) 使用Web套接字,这是可能的吗 var WebSocket = require('ws'); var ws = new WebSocket('ws://www.host.com/path
- 使用ExpresJS,我可以获得如下标题:
}))router.post('/', function (req, res, next) { console.log(req.headers);
- 使用Web套接字,这是可能的吗
var WebSocket = require('ws'); var ws = new WebSocket('ws://www.host.com/path'); ws.on('open', function open() { // how to get the headers ws.send('something'); });
谢谢WebSocket没有标题,但它们的升级请求有标题
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
console.log(ws.upgradeReq.headers);
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
注意,您不能将头设置为ws-request的一部分。WebSocket没有头,但它们的升级请求有头
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
console.log(ws.upgradeReq.headers);
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
请注意,您不能将标题设置为ws-request的一部分。WebSocket并没有标题(协议除外),这是一种观点,并没有额外的字节?WebSocket并没有标题(协议除外),这是一种观点,并没有额外的字节?谢谢,这是我一直在寻找的答案。当我们使用“new WebSocket('ws://www.host.com/path'))“要连接到websocket服务器,有没有办法传递一个可以在升级请求中检索到的头?谢谢,这是我一直在寻找的答案。当我们使用“new websocket('ws://www.host.com/path')连接到websocket服务器时,有没有办法传递一个可以在升级请求中检索到的头?”?