Initial version

This commit is contained in:
Manfred Touron 2015-08-24 18:48:27 +02:00
parent cf0c1146c7
commit 4b4cf1246d
No known key found for this signature in database
GPG Key ID: 0DCB9CE0CABAE1B5
2 changed files with 132 additions and 0 deletions

38
cmd/gotty-client/main.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/moul/gotty-client"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [GoTTY URL]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
logrus.Fatalf("GoTTY URL is missing.")
}
// create Client
client, err := gottyclient.NewClient(flag.Arg(0))
if err != nil {
logrus.Fatalf("Cannot create client: %v", err)
}
// loop
err = client.Loop()
if err != nil {
logrus.Fatalf("Communication error: %v", err)
}
}

94
gotty-client.go Normal file
View File

@ -0,0 +1,94 @@
package gottyclient
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
)
// GetWebsocketURL transforms a GoTTY http URL to its WebSocket URL
func GetWebsocketURL(httpURL string) (*url.URL, error) {
target, err := url.Parse(httpURL)
if err != nil {
return nil, err
}
if target.Scheme == "https" {
target.Scheme = "wss"
} else {
target.Scheme = "ws"
}
target.Path = strings.TrimLeft(target.Path+"ws", "/")
return target, nil
}
type Client struct {
Dialer *websocket.Dialer
Conn *websocket.Conn
Headers http.Header
Target string
Connected bool
}
// Connect tries to dial a websocket server
func (c *Client) Connect() error {
conn, _, err := c.Dialer.Dial(c.Target, c.Headers)
if err != nil {
return err
}
c.Conn = conn
return nil
}
// Close will nicely close the dialer
func (c *Client) Close() {
c.Conn.Close()
}
// Loop will look indefinitely for new messages
func (c *Client) Loop() error {
if !c.Connected {
err := c.Connect()
if err != nil {
return err
}
}
for {
_, data, err := c.Conn.ReadMessage()
if err != nil {
return err
}
switch data[0] {
case '0': // data
fmt.Print(string(data[1:]))
case '1': // new title
logrus.Infof("New title: %s", string(data[1:]))
case '2': // json prefs
logrus.Warnf("Unhandled protocol message: json pref: %s", string(data))
default:
logrus.Warnf("Unhandled protocol message: %s", string(data))
}
}
return nil
}
// NewClient returns a GoTTY client object
func NewClient(httpURL string) (*Client, error) {
target, err := GetWebsocketURL(httpURL)
if err != nil {
return nil, err
}
return &Client{
Dialer: &websocket.Dialer{},
Target: target.String(),
}, nil
}