dial a websocket

master
Jordan Orelli 4 years ago
parent 3d04042c88
commit 9abaf5a252

@ -0,0 +1,55 @@
package main
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/jordanorelli/blammo"
)
type client struct {
*blammo.Log
host string
port int
}
func (c *client) run() {
dialer := websocket.Dialer{
HandshakeTimeout: 3 * time.Second,
ReadBufferSize: 32 * 1024,
WriteBufferSize: 32 * 1024,
Subprotocols: []string{"astrodomu@v0"},
}
path := url.URL{
Host: fmt.Sprintf("%s:%d", c.host, c.port),
Scheme: "ws",
Path: "/",
}
conn, res, err := dialer.Dial(path.String(), nil)
if err != nil {
c.Error("dial error: %v", err)
return
}
c.Debug("dial response status: %d", res.StatusCode)
for k, vals := range res.Header {
c.Debug("dial response header: %s = %s", k, strings.Join(vals, ","))
}
for {
w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
c.Error("unable to get a websocket frame writer: %v", err)
break
}
w.Write([]byte("hey"))
w.Close()
time.Sleep(time.Second)
}
}

@ -10,6 +10,7 @@ require (
github.com/fatih/motion v1.1.0 // indirect github.com/fatih/motion v1.1.0 // indirect
github.com/gdamore/tcell/v2 v2.0.0 github.com/gdamore/tcell/v2 v2.0.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c
github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2
github.com/josharian/impl v1.0.0 // indirect github.com/josharian/impl v1.0.0 // indirect
github.com/jstemmer/gotags v1.4.1 // indirect github.com/jstemmer/gotags v1.4.1 // indirect

@ -114,6 +114,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c h1:Lh2aW+HnU2Nbe1gqD9SOJLJxW1jBMmQOktN2acDyJk8=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=

@ -32,14 +32,28 @@ func main() {
switch os.Args[1] { switch os.Args[1] {
case "client": case "client":
client() runClient()
case "server":
stdout := blammo.NewLineWriter(os.Stdout)
stderr := blammo.NewLineWriter(os.Stderr)
options := []blammo.Option{
blammo.DebugWriter(stdout),
blammo.InfoWriter(stdout),
blammo.ErrorWriter(stderr),
}
log := blammo.NewLog("belt", options...).Child("server")
s := server{Log: log, host: "127.0.0.1", port: 12805}
err := s.listen()
log.Error("listen error: %v", err)
default: default:
exit.WithMessage(1, "supported options are [client|server]") exit.WithMessage(1, "supported options are [client|server]")
} }
} }
func client() { func runClient() {
log := newLog("./belt.log").Child("client") log := newLog("./belt.log").Child("client")
start := time.Now() start := time.Now()
@ -55,16 +69,3 @@ func client() {
} }
ui.run() ui.run()
} }
// func server() {
// stdout := blammo.NewLineWriter(os.Stdout)
// stderr := blammo.NewLineWriter(os.Stderr)
//
// options := []blammo.Option{
// blammo.DebugWriter(stdout),
// blammo.InfoWriter(stdout),
// blammo.ErrorWriter(stderr),
// }
//
// return blammo.NewLog("belt", options...).Child("server")
// }

@ -0,0 +1,50 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/gorilla/websocket"
"github.com/jordanorelli/blammo"
)
type server struct {
*blammo.Log
host string
port int
}
func (s *server) listen() error {
return http.ListenAndServe(fmt.Sprintf("%s:%d", s.host, s.port), s)
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
s.Error("upgrade error: %v", err)
return
}
for {
t, r, err := conn.NextReader()
if err != nil {
s.Error("read error: %v", err)
return
}
switch t {
case websocket.TextMessage:
text, err := ioutil.ReadAll(r)
if err != nil {
s.Error("readall error: %v", err)
break
}
s.Info("received: %s", text)
case websocket.BinaryMessage:
}
}
}

@ -14,6 +14,13 @@ type ui struct {
} }
func (ui *ui) run() { func (ui *ui) run() {
c := client{
Log: ui.Child("client"),
host: "127.0.0.1",
port: 12805,
}
go c.run()
screen, err := tcell.NewScreen() screen, err := tcell.NewScreen()
if err != nil { if err != nil {
exit.WithMessage(1, "unable to create a screen: %v", err) exit.WithMessage(1, "unable to create a screen: %v", err)

Loading…
Cancel
Save