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 package ws
import ( import (
"bytes"
"bufio"
"io" "io"
"net" "net"
"encoding/binary" "encoding/binary"
@ -47,7 +45,7 @@ type Message struct {
// receive reads a message form reader // receive reads a message form reader
func readMessage(reader *bufio.Reader) (*Message, error){ func readMessage(reader io.Reader) (*Message, error){
var err error var err error
var tmpBuf []byte var tmpBuf []byte
@ -181,14 +179,13 @@ func (m Message) Send(socket net.Conn) error {
} }
buffer := bytes.NewBuffer(header) /* (3) Build write buffer */
writeBuf := make([]byte, 0, len(header) + int(m.Size))
/* (3) Add payload */ writeBuf = append(writeBuf, header...)
_, err := buffer.Write(m.Data) writeBuf = append(writeBuf, m.Data...)
if err != nil { return err }
/* (4) Send over socket */ /* (4) Send over socket */
_, err = io.Copy(socket, buffer) _, err := socket.Write(writeBuf)
if err != nil { return err } if err != nil { return err }
return nil return nil