Set cookie inside websocket connection

前端 未结 1 708
灰色年华
灰色年华 2021-02-05 18:25

I know that I can set cookies during handshake:

const wss = new WebSocketServer({ server, path: \'/ws\' });
wss.on(\'headers\', headers => {
  headers.push(\'         


        
相关标签:
1条回答
  • 2021-02-05 18:46

    You can't set a cookie upon receipt of a webSocket message. Once the webSocket connection has been established, it's an open TCP socket and the protocol is no longer http, thus there is no built-in way to exchange cookies.

    You can send your own webSocket message back to the client that tells it to set a cookie and then be listening for that message in the client and when it gets that message, it can set the cookie in the browser.

    0 讨论(0)
提交回复
热议问题