2018-10-19 08:09
浏览 170

Golang WebSocket数据不正确

I'm using to read data from a websocket. The data is relatively large. When I read it, I can't read it completely, so it is cut off. Is there any way to solve it?

func receiveWebsocket(ws *websocket.Conn) error {
    for {
        var msg = make([]byte, 1024*1024) // 1024kb
        m, err := ws.Read(msg) 
        if err != nil {
            log15.Error("ws read error", "error", err)
            return err
        fmt.Println("length ---",m,  string(msg))
        response := string(msg[:m])

According to the log, the m value is always 4092, even if msg is very large.

图片转代码服务由CSDN问答提供 功能建议

我正在使用 从websocket读取数据。 数据比较大。 当我阅读它时,我无法完全阅读它,因此它被切断了。 有什么方法可以解决?

  func receiveWebsocket(ws * websocket.Conn)错误{
 for {
 var msg = make([] byte,1024 * 1024)// 1024kb 
m,  err:= ws.Read(msg)
如果err!= nil {
 log15.Error(“ ws读取错误”,“ error”,err)
 fmt.Println(“ length  ---“,m,字符串(msg))
响应:=字符串(msg [:m])
 \  n 

根据日志,即使 msg 非常大, m 值始终为4092。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dounianluo0086 2018-10-19 11:38

    First call always reads less than 4KB, and for security reasons the length is limited, if you want to increase, set:

    ws.MaxPayloadBytes = xxxx

    From the second call your msg will be filled.

    If you want to read the whole message in a single call,do:

    msg, err := ioutil.ReadAll(ws)
    打赏 评论

相关推荐 更多相似问题