duangeli1334 2018-09-21 20:15
浏览 198
已采纳

HTTP处理函数返回后,如何保持WebSocket连接打开?

I'm trying to write code to stream data on a topic, like a radio station (one broadcaster, several listeners). I'm stuck on how to process a new WebSocket connection request without having a goroutine for each open WebSocket (that starts to get resource-intensive for many "listeners" to the same "station").

At the moment, I have a map of dataStream structs which look like this:

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

And here's the pseudocode for upgrading the request to a WebSocket, and then attempting to add a WebSocket connection to the 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)
  }
}

And then here's the stream function mentioned in the above code block. One of these runs in the background for each dataStream (not for every WebSocket connection).

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
  }
}

The trouble with this approach is that in the process_request function, as soon as the flow reaches the bottom if statement of the 2nd and subsequent connections, after the new connection is appended to the dataStream.conns slice the function terminates closing the WebSocket connection! As a result, stream() is running in the background and polls that a closed connection has been added to the ds.conns slice and removes it.

Hence my question:

What approach should I take to maintain the WebSocket connection open for even after the process_request handler function returns, preferentially without running a separate goroutine for each connection?

  • 写回答

1条回答

  • doxzrb8721 2018-09-21 20:43
    关注

    The application must explicitly close a Gorilla connection. The connection is not automatically closed when the HTTP handler function returns.

    In this case, the application uses a defer statement to close the connection on return from the handler. Delete the defer statement to avoid closing the connection.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘