made it more generic (io.Reader, simple Write, etc)

This commit is contained in:
xdrm-brackets 2018-05-05 17:39:39 +02:00
parent 009f34e436
commit 858d409834
1 changed files with 6 additions and 9 deletions

View File

@ -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