Golang WebSocket数据不正确

我正在使用 x.net.websocket </ code>从websocket读取数据。

数据比较大。 当我阅读它时,我无法完全阅读它,因此它被切断了。
有什么方法可以解决? </ p>

  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)
返回err
}
fmt.Println(“ length ---“,m,字符串(msg))
响应:=字符串(msg [:m])
分配Websocket(响应)
}
}
</ code> </ pre>
\ n

根据日志,即使 msg </ code>非常大, m </ code>值始终为4092。</ p>
</ div>

展开原文

原文

I'm using x.net.websocket 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])
        assignmentWebsocket(response)
    }
}

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

duanjiashai9140
duanjiashai9140 示例:gist.github.com/bingqianglin/7c7af34eb53f68efab5457dd7d631f4b
接近 2 年之前 回复
dswm97353
dswm97353 我用这种方法
接近 2 年之前 回复

1个回答



首次调用的读取始终小于4KB,并且出于安全原因,长度受到限制,如果要增加,请设置:</ p >

  ws.MaxPayloadBytes = xxxx 
</ code> </ pre>

从第二个调用开始,您的味精将被填充。</ p>
\ n

如果要在单个调用中读取整个消息,请执行以下操作:</ p>

  msg,err:= ioutil.ReadAll(ws)
</ code> </ pre>
</ div>

展开原文

原文

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)

doubeishuai6598
doubeishuai6598 我发现此设置不起作用,最后,我使用了这个lib github.com/gorilla/websocket,它现在可以工作了。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问