HTTP headers in Websockets client API

后端 未结 11 2449
情话喂你
情话喂你 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:48

    You can pass the headers as a key-value in the third parameter (options) inside an object. Example with Authorization token. Left the protocol (second parameter) as null

    
    ws = new WebSocket(‘ws://localhost’, null, { headers: { Authorization: token }})
    
    

    Edit: Seems that this approach only works with nodejs library not with standard browser implementation. Leaving it because it might be useful to some people.

提交回复
热议问题