douziqian2871 2015-07-09 08:45
浏览 298
已采纳

Gorilla Websocket错误:关闭1007 UTF-8序列非法

I'm trying to implement a websocket proxy server for GlassFish. If I try to connect more than one client I'm getting error:

ReadMessage Failed: websocket: close 1007 Illegal UTF-8 Sequence.

I'm sure the GlassFish server sending right data, because the same server works properly with another proxy server implemented with node.js.

func GlassFishHandler(conn *websocket.Conn){

    defer conn.Close()

    conn.SetReadDeadline(time.Now().Add(1000 * time.Second))
    conn.SetWriteDeadline(time.Now().Add(1000 * time.Second))

    fmt.Println("WS-GOLANG PROXY SERVER: Connected to GlassFish")

    for {

        messageType, reader, err := conn.NextReader()

        if err != nil {
            fmt.Println("ReadMessage Failed: ", err) // <- error here
        } else {

            message, err := ioutil.ReadAll(reader)
            if (err == nil && messageType == websocket.TextMessage){

                var dat map[string]interface{}
                if err := json.Unmarshal(message, &dat); err != nil {
                    panic(err)
                } 

                // get client destination id
                clientId := dat["target"].(string)

                fmt.Println("Msg from GlassFish for Client: ", dat);

                // pass through
                clients[clientId].WriteMessage(websocket.TextMessage, message)
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dsfasdfsda234234 2015-07-09 11:33
    关注

    Summing up my comments as an answer:

    When you are writing to the client, you are taking the clientId from the GlassFish message, fetching the client from a map, and then writing to it - basically clients[clientId].WriteMessage(...).

    While your map access can be thread safe, writing is not, as this can be seen as:

    // map access - can be safe if you're using a concurrent map
    client := clients[clientId]
    
    // writing to a client, not protected at all
    client.WriteMessage(...)
    

    So what's probably happening is that two separate goroutines are writing to the same client at the same time. You should protect your client from it by adding a mutex in the WriteMessage method implementation.

    BTW actually instead of protecting this method with a mutex, a better, more "go-ish" approach would be to use a channel to write the message, and a goroutine per client that consumes from the channel and writes to the actual socket.

    So in the client struct I'd do something like this:

    type message struct {
       msgtype string
       msg string
     }
    
    type client struct {
        ...
        msgqueue chan *message
    }
    
    
    func (c *client)WriteMessage(messageType, messageText string) {
       // I'm simplifying here, but you get the idea
       c.msgqueue <- &message{msgtype: messageType, msg: messageText}
    }
    
    func (c *client)writeLoop() {
       go func() {
           for msg := ragne c.msgqueue {
               c.actuallyWriteMessage(msg)
           }
       }()
    }
    

    and when creating a new client instance, just launch the write loop

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

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。