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

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

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。