node.js socket.io-client 如何在建立连接时设置请求头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23406163/
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
socket.io-client how to set request header when making connection
提问by Ziyu
I'm trying to set a http header when socket.io client makes the connection request. Is there a way to do this?
当 socket.io 客户端发出连接请求时,我试图设置一个 http 标头。有没有办法做到这一点?
Here is what i'm doing:
这是我在做什么:
// server side
var io = socketio(server);
io.use(function (socket, next) {
// authorize using authorization header in socket.request.headers
});
// client side
var socket = io(); // i'm trying to set an authorization header in this http reqeust
Any ideas? Thanks.
有任何想法吗?谢谢。
回答by ymyzk
You can use extraHeadersoption, if you are using socket.io-client >= 1.4.
extraHeaders如果您使用的是 socket.io-client >= 1.4,则可以使用选项。
For example:
例如:
var socket = io("http://localhost", {
extraHeaders: {
Authorization: "Bearer authorization_token_here"
}
});
engine.io-client, which is a backend of socket.io-client, introduced extraHeaderssupport on 2015-11-28.
engine.io-client是 socket.io-client 的后端,在 2015-11-28引入了extraHeaders支持。
回答by bakavic
It seems like the client doesn't support setting headers, as not all transports allow for the setting of headers.
似乎客户端不支持设置 headers,因为并非所有传输都允许设置 headers。
This postby facundoolano details a workaround to authentication that doesn't require placing the auth token in the query string.
facundoolano 的这篇文章详细介绍了一种无需在查询字符串中放置身份验证令牌的身份验证解决方法。
His workaround module can be found at https://github.com/invisiblejs/socketio-auth.
他的解决方法模块可以在https://github.com/invisiblejs/socketio-auth找到。
Makes me wonder why on server-side, socket.io allows for the request headers to be accessed...
让我想知道为什么在服务器端,socket.io 允许访问请求标头......
回答by Lu4
As of version 2.0.0 / 2017-01-22 engine.io-client supports
从版本 2.0.0 / 2017-01-22 engine.io-client支持
[feature] Allow extraHeaders to be set for browser clients in XHR requests (#519)
However at this point the socket.io-client is not updated to support this functionality, so couple of days may make this saga end until that time use the following instructions: https://facundoolano.wordpress.com/2014/10/11/better-authentication-for-socket-io-no-query-strings/
但是,此时 socket.io-client 尚未更新以支持此功能,因此可能需要几天时间才能结束此传奇,直到那时使用以下说明:https: //facundoolano.wordpress.com/2014/10/11 /better-authentication-for-socket-io-no-query-strings/
回答by adamrights
This information has been deprecated since socket.io 1.0
此信息自 socket.io 1.0 起已被弃用
There are two methods of authorization: global or namespace (think route). The global method is set on the server with the io.set('authorization', function (handshakeData, callback)configuration call.
有两种授权方法:全局或命名空间(想想路由)。全局方法通过io.set('authorization', function (handshakeData, callback)配置调用在服务器上设置。
The handshakeData object contains the following information:
握手数据对象包含以下信息:
{
headers: req.headers // <Object> the headers of the request
, time: (new Date) +'' // <String> date time of the connection
, address: socket.address() // <Object> remoteAddress and remotePort object
, xdomain: !!headers.origin // <Boolean> was it a cross domain request?
, secure: socket.secure // <Boolean> https connection
, issued: +date // <Number> EPOCH of when the handshake was created
, url: request.url // <String> the entrance path of the request
, query: data.query // <Object> the result of url.parse().query or a empty object
}
The above information and a deeper explanation is available on this documentation page.
此文档页面上提供了上述信息和更深入的解释。
回答by jagjeet
"transportOptions" options can be used to send extra headers in socket.io request. I also explained that here :-
“transportOptions”选项可用于在 socket.io 请求中发送额外的标头。我也在这里解释过:-

