Return min length of buffers

Signed-off-by: Xiaoguang Sun <sunxiaoguang@gmail.com>
This commit is contained in:
Xiaoguang Sun 2022-09-15 21:51:37 +08:00
parent 01668a5425
commit 6522d4b241

View file

@ -17,6 +17,14 @@ func (wsw *wsWrapper) Write(p []byte) (n int, err error) {
return writer.Write(p)
}
func minInt(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func (wsw *wsWrapper) Read(p []byte) (n int, err error) {
for {
msgType, bytes, err := wsw.Conn.ReadMessage()
@ -29,6 +37,6 @@ func (wsw *wsWrapper) Read(p []byte) (n int, err error) {
}
copy(p, bytes)
return len(bytes), err
return minInt(len(p), len(bytes)), err
}
}