Custom headers for WebSocket JS

前端 未结 2 2126
遥遥无期
遥遥无期 2021-02-19 06:40

I finding a simple solution to use WebSocket with custom headers for web app based on PHP as backend and js+vuejs as frontend.

My app should connect to WebSocket server

相关标签:
2条回答
  • 2021-02-19 07:02

    Websocket client API doesn't allow to send custom header, they allow you to set exactly one header, namely Sec-WebSocket-Protocol, i.e. the application specific subprotocol. You could use this header for passing the bearer token.

    Other option is to use token in query string.

    Or you can use some library on client or server which support custom headers implementation eg. https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_httpheaders

    0 讨论(0)
  • 2021-02-19 07:06

    I'm so confused at the massive amount of mis-information of websockets and headers. There is a way to add headers to WebSocket connections.

    Like this

    options.headers= {"header1":"value"}
    const socket = new WebSocket('wss://yourwebsocketAPIpath', [],options )
    
    0 讨论(0)
提交回复
热议问题