Added write support

This commit is contained in:
Manfred Touron 2015-08-24 22:53:35 +02:00
parent ee8195320f
commit e1e185cc7f
No known key found for this signature in database
GPG Key ID: 0DCB9CE0CABAE1B5
1 changed files with 34 additions and 2 deletions

View File

@ -1,10 +1,13 @@
package gottyclient package gottyclient
import ( import (
"bufio"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
"unicode/utf8"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket" "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 { for {
_, data, err := c.Conn.ReadMessage() _, data, err := c.Conn.ReadMessage()
if err != nil { if err != nil {
return err done <- true
logrus.Warnf("c.Conn.ReadMessage: %v", err)
return
} }
switch data[0] { switch data[0] {
@ -78,7 +111,6 @@ func (c *Client) Loop() error {
logrus.Warnf("Unhandled protocol message: %s", string(data)) logrus.Warnf("Unhandled protocol message: %s", string(data))
} }
} }
return nil
} }
// NewClient returns a GoTTY client object // NewClient returns a GoTTY client object