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

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

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?