socket.io-client建立连接时如何设置请求头 [英] socket.io-client how to set request header when making connection

查看:4338
本文介绍了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

有什么想法吗?谢谢.

推荐答案

您可以使用 选项,如果您使用的是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的后端,

engine.io-client, which is a backend of socket.io-client, introduced extraHeaders support on 2015-11-28.

这篇关于socket.io-client建立连接时如何设置请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆