douziqian2871
2015-07-09 08:45 阅读 165
已采纳

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 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

    点赞 评论 复制链接分享

相关推荐