如何在GO中存储WebSocket连接

huangapple go评论144阅读模式
英文:

How to store websocket connection in GO

问题

我想将客户端的 WebSocket 连接存储到 wsList 中,并统一发送响应。但是它会返回 "use of closed network connection"。如何修复这个问题?

  1. import (
  2. "code.google.com/p/go.net/websocket"
  3. ...
  4. )
  5. var wsList []*websocket.Conn
  6. func WShandler(ws *websocket.Conn) {
  7. wsList = append(wsList, ws)
  8. go sendmsg()
  9. }
  10. func sendmsg() {
  11. for _, conn := range wsList {
  12. if err := websocket.JSON.Send(conn, outmsg); err != nil {
  13. fmt.Printf("%s", err) // "use of closed network connection"
  14. }
  15. }
  16. }

你可以在 sendmsg 函数中添加错误处理,检查连接是否已关闭。可以使用 websocket.JSON.Send 方法的返回值来判断连接是否关闭,如果连接已关闭,则不再发送消息。例如:

  1. func sendmsg() {
  2. for _, conn := range wsList {
  3. if websocket.JSON.Send(conn, outmsg) != nil {
  4. if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
  5. // 连接已关闭,不再发送消息
  6. continue
  7. }
  8. fmt.Printf("%s", err)
  9. }
  10. }
  11. }

这样,当连接已关闭时,就不会再尝试发送消息,避免了 "use of closed network connection" 的错误。

英文:

I want to store client websocket connection into wsList, and send response in uniform. but it will return "use of closed network connection". How to fix it?

  1. import {
  2. "code.google.com/p/go.net/websocket"
  3. ...
  4. }
  5. var wsList []*websocket.Conn
  6. func WShandler(ws *websocket.Conn) {
  7. wsList = append(wsList, ws)
  8. go sendmsg()
  9. }
  10. func sendmsg() {
  11. for _, conn := range wsList {
  12. if err := websocket.JSON.Send(conn, outmsg); err != nil {
  13. fmt.Printf("%s", err) //"use of closed network connection"
  14. }
  15. }
  16. }

答案1

得分: 3

连接wsWsHandler返回时关闭。为了解决这个问题,可以通过在循环中读取消息直到检测到错误来防止WsHandler返回:

  1. func WShandler(ws *websocket.Conn) {
  2. wsList = append(wsList, ws)
  3. go sendmsg()
  4. for {
  5. var s string
  6. if err := websocket.Message.Receive(ws, &s); err != nil {
  7. break
  8. }
  9. }
  10. // 在这里从wsList中移除ws
  11. }

wsList存在竞态条件。可以使用互斥锁来保护它。

英文:

The connection ws is closed when WsHandler returns. To fix the problem, prevent WsHandlerfrom returning by reading messages in a loop until an error is detected:

  1. func WShandler(ws *websocket.Conn) {
  2. wsList = append(wsList, ws)
  3. go sendmsg()
  4. for {
  5. var s string
  6. if err := websocket.Message.Receive(ws, &s); err != nil {
  7. break
  8. }
  9. }
  10. // remove ws from wsList here
  11. }

There's a race on wsList. Protect it with a mutex.

答案2

得分: 2

你不能简单地假设所有的连接会无限期地保持打开状态,因为另一端可能会随意关闭它们,或者可能发生网络故障,迫使它们关闭。

当你尝试读取或写入一个已关闭的连接时,会出现错误信息:

  1. "使用已关闭的网络连接"

你应该丢弃已关闭的连接。

英文:

You cannot simply assume all connections to stay open indefinitely because the other end may close them at will or a network outage may occur, forcing them to close as well.

When you try to read or write to a closed connection, you get an error

  1. "use of closed network connection"

You should discard closed connections.

huangapple
  • 本文由 发表于 2014年5月12日 19:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/23607811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定