【问题标题】:How to maintain WebSocket connections open after HTTP handler function returns?HTTP处理函数返回后如何保持WebSocket连接打开?
【发布时间】:2018-09-22 04:43:45
【问题描述】:

我正在尝试编写代码以流式传输有关某个主题的数据,例如广播电台(一个广播公司,多个听众)。我被困在如何处理一个新的 WebSocket 连接请求,而不需要为每个打开的 WebSocket 设置一个 goroutine(对于许多“侦听器”到同一个“站”来说,这开始占用资源)。

目前,我有一张 dataStream 结构图,如下所示:

struct dataStream {
  data chan byte[]
  conns []*websocket.Connection
}

这是将请求升级到 WebSocket,然后尝试将 WebSocket 连接添加到 dataStreams conns 的伪代码:

func process_request(w http.ResponseWriter, r *http.Request) {
  // hundred lines of business logic...
  c := upgrade websocket connection
  defer c.Close()
  if dataStream exists {
    append the new connection c to the dataStream.conns slice
  } else {
    create new dataStream
    append the new connection c to the dataStream.conns slice
    stream(dataStream)
  }
}

然后是上面代码块中提到的stream函数。其中一个在后台为每个 dataStream 运行(不是每个 WebSocket 连接)。

func stream(ds *dataStream) {
  ticker := time.NewTicker(poll every ~10 seconds)
  go func() { // this is to poll and remove closed connections
  for _ = range ticker.C {
    for traverse ds.conns {
      ping all connections, remove any closed ones and free memory
      if len(ds.conns == 0){ // no more connections are listening to this dataStream
        delete the ds dataStream and free the memory
        stop ticker
        return // kill goroutine and free the memory
      }
    }
  }}()
  while len(ds.conns) != 0 { // while there are open connections
    fetch any available <-ds.data from channel
    write the data as websocket message to each connection
  }
}

这种方法的问题在于,在process_request 函数中,一旦流到达第二个和后续连接的底部if statement,在新连接附加到dataStream.conns 切片后,函数就会终止关闭 WebSocket 连接! 结果,stream() 在后台运行并轮询已关闭的连接已添加到 ds.conns 切片并将其删除。

因此我的问题是:

即使在 process_request 处理函数返回后,我应该采取什么方法来保持 WebSocket 连接打开,最好不要为每个连接运行单独的 goroutine?

【问题讨论】:

  • net/http 请求处理程序用于处理 HTTP 请求,而不是用于管理 WebSocket 连接。查看godoc.org/golang.org/x/net/websocket 以获得标准库解决方案,或者查看从该文档链接的库以获得更完整的第三方解决方案。
  • 如果您使用 Gorilla,则返回 process_request 不会关闭连接。其他东西可能会关闭连接,但从处理程序返回不是原因。因为您不共享实际代码,所以很难评论可能出错的地方。我建议使用 race detector 运行应用程序并修复它报告的任何问题。
  • @Adrian 我按照推荐使用 Gorilla 库,并且我了解到升级 http 请求是正常做法。
  • @ThunderCat 实际代码大约 750 行,所以我使用了伪代码。问题在于处理 http 请求并将其转换为 websocket 连接的方式,而不是语法本身(编译得很好)。
  • 几乎不可能用伪代码解决特定问题。请尝试创建一个(最小、完整、可验证的示例)[stackoverflow.com/help/mcve] 展示您的问题。

标签: go websocket


【解决方案1】:

应用程序必须明确关闭 Gorilla 连接。当 HTTP 处理函数返回时,连接不会自动关闭。

在这种情况下,应用程序使用 defer 语句在处理程序返回时关闭连接。删除 defer 语句,避免关闭连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 2014-03-16
    • 2017-08-15
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多