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