dsideal2015 2018-03-11 15:39
浏览 188

获取Websocket会话

My idea is as follows.

When connection from client-side (browser) is requested, server generates random token on websocket upgrade request and sends it via HTTP Set-Cookie header. Then, client authenticates via Challenge-Response authentication over websocket connection. On successful auth, server saves previously generated token in database for user. token cookie is needed to restore session, when user re-initiates websocket connection.

My question is, how can I temporarily store token on memory per connection (per user), so that I can access it later and save in database for that user? (sort of "request context").

  • 写回答

1条回答 默认 最新

  • doulu8847 2018-03-19 15:53
    关注

    you could do something like this

    // initialize an empty map
    var Clients = make(map[*websocket.Conn]bool)
    

    and then add the connection or token to the map like this

    // upgrade connection to websocket and add it to the map
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        fmt.Println(err)
        return
    }
    Clients[conn] = true
    
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程