From e1e185cc7f4add444498017ce3922048d11e279e Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Mon, 24 Aug 2015 22:53:35 +0200 Subject: [PATCH] Added write support --- gotty-client.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/gotty-client.go b/gotty-client.go index 50728f4..4de4ef8 100644 --- a/gotty-client.go +++ b/gotty-client.go @@ -1,10 +1,13 @@ package gottyclient import ( + "bufio" "fmt" "net/http" "net/url" + "os" "strings" + "unicode/utf8" "github.com/Sirupsen/logrus" "github.com/gorilla/websocket" @@ -60,10 +63,40 @@ func (c *Client) Loop() error { } } + done := make(chan bool) + go c.readLoop(done) + go c.writeLoop(done) + <-done + return nil +} + +func (c *Client) writeLoop(done chan bool) { + reader := bufio.NewReader(os.Stdin) + for { + x, size, err := reader.ReadRune() + if size <= 0 || err != nil { + done <- true + return + } + + p := make([]byte, size) + utf8.EncodeRune(p, x) + + err = c.Conn.WriteMessage(websocket.TextMessage, append([]byte("0"), p...)) + if err != nil { + done <- true + return + } + } +} + +func (c *Client) readLoop(done chan bool) { for { _, data, err := c.Conn.ReadMessage() if err != nil { - return err + done <- true + logrus.Warnf("c.Conn.ReadMessage: %v", err) + return } switch data[0] { @@ -78,7 +111,6 @@ func (c *Client) Loop() error { logrus.Warnf("Unhandled protocol message: %s", string(data)) } } - return nil } // NewClient returns a GoTTY client object