From 858d4098341d7546fdb2fd52613f3ac41c0d4739 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 5 May 2018 17:39:39 +0200 Subject: [PATCH] made it more generic (io.Reader, simple Write, etc) --- ws/message.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/ws/message.go b/ws/message.go index 026609e..8ca95ea 100644 --- a/ws/message.go +++ b/ws/message.go @@ -1,8 +1,6 @@ package ws import ( - "bytes" - "bufio" "io" "net" "encoding/binary" @@ -47,7 +45,7 @@ type Message struct { // receive reads a message form reader -func readMessage(reader *bufio.Reader) (*Message, error){ +func readMessage(reader io.Reader) (*Message, error){ var err error var tmpBuf []byte @@ -181,14 +179,13 @@ func (m Message) Send(socket net.Conn) error { } - buffer := bytes.NewBuffer(header) - - /* (3) Add payload */ - _, err := buffer.Write(m.Data) - if err != nil { return err } + /* (3) Build write buffer */ + writeBuf := make([]byte, 0, len(header) + int(m.Size)) + writeBuf = append(writeBuf, header...) + writeBuf = append(writeBuf, m.Data...) /* (4) Send over socket */ - _, err = io.Copy(socket, buffer) + _, err := socket.Write(writeBuf) if err != nil { return err } return nil