made it more generic (io.Reader, simple Write, etc)
This commit is contained in:
parent
009f34e436
commit
858d409834
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue