源自:4-3 封装Websocket
以下代码为啥会存在线程安全的问题?
func WsHandle(writer http.ResponseWriter, request *http.Request) {
var (
conn *websocket.Conn
err error
messageType int
data []byte
)
if conn, err = upgrader.Upgrade(writer, request, nil); err != nil {
goto ERR
}
for {
fmt.Printf("connection: %v", conn)
if messageType, data, err = conn.ReadMessage(); err != nil {
goto ERR
}
switch messageType {
case websocket.TextMessage:
fmt.Println(string(data))
if err = conn.WriteMessage(websocket.TextMessage, data); err != nil {
goto ERR
}
break
case websocket.BinaryMessage:
break
}
}
ERR:
conn.Close()
}
提问者:qq_文er不二_acijms
2022-02-02 19:48