dongwa3808 2019-07-22 07:04
浏览 83
已采纳

WebSockets和HTTP标头

I am trying to figure out how to add a header with an authorization token to a WebSocket connection, after it has been established. Basically, to use a cookie in order to validate each message in the connection.

There is a great discussion at HTTP headers in Websockets client API already, yet it did not really help me.

One of the the suggestions there was to do:

document.cookie = 'X-Authorization=' + token + '; path=/';

Unfortunately, it did not help me.

func handler(ws *websocket.Conn, h *Hub) {
    go h.run()
    h.addClientChan <- ws
    for {
        config := ws.Config()
        fmt.Println("Headers length:", len(config.Header))
        ...

Shows a map with 0 elements before and after I add the cookie.

Is it possible to alter WebSocket connection headers, after the connection has been established?

Do I even need to add an authorization token to a WebSocket connection, provided I authenticate each time, or is it only useful to re-establish a connection and just a useless check after the fact?

  • 写回答

1条回答 默认 最新

  • doulong6761 2019-07-22 07:36
    关注

    Is it possible to alter WebSocket connection headers, after the connection has been established?

    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 use authorizathion on the first http request, where both sides establish protocol for exchange data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏