socket.io-client 如何在建立连接时设置请求头 [英] socket.io-client how to set request header when making connection
本文介绍了socket.io-client 如何在建立连接时设置请求头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 socket.io 客户端发出连接请求时设置 http 标头.有没有办法做到这一点?
I'm trying to set a http header when socket.io client makes the connection request. Is there a way to do this?
这是我在做什么:
// 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
有什么想法吗?谢谢.
推荐答案
您可以使用 extraHeaders
选项,如果您使用的是 socket.io-client >= 1.4.
You can use extraHeaders
option, if you are using socket.io-client >= 1.4.
例如:
var socket = io("http://localhost", {
extraHeaders: {
Authorization: "Bearer authorization_token_here"
}
});
engine.io-client,是socket.io-client的后端,在 2015-11- 引入了 extraHeaders
支持-28.
engine.io-client, which is a backend of socket.io-client, introduced extraHeaders
support on 2015-11-28.
这篇关于socket.io-client 如何在建立连接时设置请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文