doumi1099 2019-06-15 19:19
浏览 621

并发写入websocket连接

I am using github.com/gorilla/websocket for websockets. I have this code

type CONN struct {
    Conn *websocket.Conn
    Username string
    hand []string
    mu *sync.Mutex
}
func (c *CONN) Send(messageType int, message []byte) error {
    c.mu.Lock()
    defer c.mu.Unlock()
    return c.Conn.WriteMessage(messageType, message)
}
//later...
connection.Send(messageType, []byte("play"))
connection.Send(messageType, []byte("[\"a\", \"b\", \"c\"]"))

These last two lines are giving me the error. Please let me know if you need any more code to figure it out. Here's the error:

2019/06/15 15:19:03 http: panic serving [::1]:56788: concurrent write to websocket connection
goroutine 10 [running]:
net/http.(*conn).serve.func1(0xc000164140)
    /usr/lib64/go/1.11/src/net/http/server.go:1746 +0xd0
panic(0x6654e0, 0x7125a0)
    /usr/lib64/go/1.11/src/runtime/panic.go:513 +0x1b9
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc000043a18, 0xc000016501, 0xc00001656c, 0x0, 0x4, 0x713e00, 0xc0001141e0)
    /home/victor/programs/goprograms/src/github.com/gorilla/websocket/conn.go:597 +0x631
github.com/gorilla/websocket.(*Conn).WriteMessage(0xc0001a4160, 0x1, 0xc000016568, 0x4, 0x8, 0x4, 0xc000016568)
    /home/victor/programs/goprograms/src/github.com/gorilla/websocket/conn.go:753 +0x24b
main.(*CONN).Send(0xc00015c280, 0x1, 0xc000016568, 0x4, 0x8, 0x0, 0x0)
    /home/victor/programs/goprograms/src/v/asdf/network/main.go:32 +0x9e
main.wsHandler(0x716960, 0xc00011e380, 0xc000116400)
    /home/victor/programs/goprograms/src/v/asdf/network/main.go:68 +0x501
net/http.HandlerFunc.ServeHTTP(0x6e1ab0, 0x716960, 0xc00011e380, 0xc000116400)
    /usr/lib64/go/1.11/src/net/http/server.go:1964 +0x44
net/http.(*ServeMux).ServeHTTP(0x8dadc0, 0x716960, 0xc00011e380, 0xc000116400)
    /usr/lib64/go/1.11/src/net/http/server.go:2361 +0x127
net/http.serverHandler.ServeHTTP(0xc00008d040, 0x716960, 0xc00011e380, 0xc000116400)
    /usr/lib64/go/1.11/src/net/http/server.go:2741 +0xab
net/http.(*conn).serve(0xc000164140, 0x716b60, 0xc00005a800)
    /usr/lib64/go/1.11/src/net/http/server.go:1847 +0x646
created by net/http.(*Server).Serve
    /usr/lib64/go/1.11/src/net/http/server.go:2851 +0x2f5

Here is the full code I am using - line 75 is where the connection is set. I don't see how there could be multiple connections under one item in the map.

  • 写回答

1条回答 默认 最新

  • dongqianchi0512 2019-06-22 06:32
    关注

    Your func wsHandler(w http.ResponseWriter, r *http.Request) is not properly synchronized over the global variable map connections

    Each call to wsHandler is made in a separate goroutine:

    From https://golang.org/pkg/net/http/#Server.ListenAndServe

    ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections.

    And from https://golang.org/pkg/net/http/#Server.Serve

    Serve accepts incoming connections on the Listener l, creating a new service goroutine for each.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog