Say I have some WS code like so:
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
for {
messageType, message, err := ws.ReadMessage()
// above works...
// I want to read the first 20 bytes, not the whole message
}
}
how an I read the first X bytes, not the whole message?
Use NextReader to get an io.Reader on the message. Read X bytes from the reader.
If you want to handle short messages as an error, then change the error check to: