dongpou7275 2018-01-18 05:27
浏览 125
已采纳

如果我不在任何地方读取消息,则不会为大猩猩/ websocket调用CloseHandler,最终我只会得到写错误

I have a websocket server using gorilla/websocket.

I have a situation where I am simply writing messages to a set of websockets. My custom CloseHandler is never called when I close the websocket on the browser side.

However, adding a goroutine that calls ReadMessage indefinitely (till some error) leads to the CloseHandler being invoked.

Here's the basic idea: In one goroutine, I run something like this:

for {
    for client := range clients {
        client.stream <- data
    }
    time.Sleep(time.Second)
}

and the other code, called in a separate goroutine, one per client:

go (func() {
    // If I call wsock.ReadMessage here, my CloseHandler works!
})()

for msg := range myclient.stream {
    if err := wsock.WriteMessage(websocket.TextMessage, msg); err != nil {
        break
    }
}

When I close the websocket on the browser side, I expect the CloseHandler to be called, however, it's never called, instead, I eventually get an error on the WriteMessage call.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在数字信号处理中用MATLAB显示波形。
      • ¥15 keil MDK软件进行仿真调试的局限
      • ¥15 关于gateway产生临时文件夹的问题
      • ¥15 Java Broken Pipe以及OOM
      • ¥20 请问有没有RTL8211FS的参考原理图
      • ¥50 esp32的platfromio工程添加文件就会编译失败
      • ¥15 linux下运行VASP报错
      • ¥15 python里面查找赋值,如何可以在剩余未空值的行里面进行进一步的筛选呢?
      • ¥15 电路分析的综合实验有没有会的
      • ¥15 #硬件设计#PNP三级管输出设计问题