HTTP headers in Websockets client API

后端 未结 11 2435
情话喂你
情话喂你 2020-11-22 10:41

Looks like it\'s easy to add custom HTTP headers to your websocket client with any HTTP header client which supports this, but I can\'t find how to do it with the JSON API.

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 10:53

    More of an alternate solution, but all modern browsers send the domain cookies along with the connection, so using:

    var authToken = 'R3YKZFKBVi';
    
    document.cookie = 'X-Authorization=' + authToken + '; path=/';
    
    var ws = new WebSocket(
        'wss://localhost:9000/wss/'
    );
    

    End up with the request connection headers:

    Cookie: X-Authorization=R3YKZFKBVi
    

提交回复
热议问题