Node.js + Socket.io | Set custom headers on the server

后端 未结 1 562
暖寄归人
暖寄归人 2021-01-14 04:43

I use Helmet with Express to set quite some security HTTP headers from the server side. This is nicely done, when rendering client pages on top of the node.js app, using:

相关标签:
1条回答
  • 2021-01-14 05:27

    extraHeaders options will work as below, as you need to remove "transports: ['polling']," in case you are using, and use below pattern. This worked for me, and was able to send custom headers.

    package used :- "socket.io-client": "^2.2.0",

    this.socket = io(environment.host, {
       path: `/api/backend/socket.io`,
       origins: '*:*',
       // transports: ['polling'],
       transportOptions: {
         polling: {
            extraHeaders: {
               'authorization': token,
               'user-id' : userId
            }
         }
        }
     })
    

    Ref:- https://socket.io/docs/client-api/#With-extraHeaders

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