mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-26 13:44:24 +00:00
42 lines
726 B
Go
42 lines
726 B
Go
package server
|
|
|
|
import (
|
|
"io/ioutil"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type wsWrapper struct {
|
|
*websocket.Conn
|
|
}
|
|
|
|
func (wsw *wsWrapper) Write(p []byte) (n int, err error) {
|
|
writer, err := wsw.Conn.NextWriter(websocket.TextMessage)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer writer.Close()
|
|
return writer.Write(p)
|
|
}
|
|
|
|
func (wsw *wsWrapper) Read(p []byte) (n int, err error) {
|
|
for {
|
|
msgType, reader, err := wsw.Conn.NextReader()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if msgType != websocket.TextMessage {
|
|
continue
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(reader)
|
|
if len(b) > len(p) {
|
|
return 0, errors.Wrapf(err, "Client message exceeded buffer size")
|
|
}
|
|
n = copy(p, b)
|
|
return n, err
|
|
}
|
|
}
|