websocket请求头中如何带上cookie?

带cookie,是为了带token吗,我这有一段ws的带token的方法。

ws = new WebSocket(url, [store.getters.token]);
服务端
  List authorization = headers.get("Sec-WebSocket-Protocol");

1. 如果是相同域名的 websocket url,那么可以直接设置 document.cookies,浏览器会在请求的时候自动携带这个 cookies

var authToken = 'R3YKZFKBVi';

document.cookie = 'X-Authorization=' + authToken + '; path=/';

var ws = new WebSocket(
    'wss://localhost:9000/wss/'
);

2. 可以使用 HTTP Auth 的方法,也就是将 auth 信息写在 uri 中

var ws = new WebSocket("ws://username:[email protected]")
3. 使用 SocketIO 作为替代方案
var socket = io("http://localhost", {
  extraHeaders: {
    Authorization: "Bearer authorization_token_here"
  }
});

回复

你可能感兴趣的:(websocket,网络协议,网络)