You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.4 KiB
Go

4 years ago
package main
import (
"context"
4 years ago
"encoding/json"
4 years ago
"fmt"
"io/ioutil"
4 years ago
"net/url"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/jordanorelli/astro-domu/internal/wire"
4 years ago
"github.com/jordanorelli/blammo"
)
type client struct {
*blammo.Log
4 years ago
host string
port int
lastSeq int
conn *websocket.Conn
outbox chan wire.Request
4 years ago
}
func (c *client) run(ctx context.Context) {
c.outbox = make(chan wire.Request)
4 years ago
4 years ago
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
}
4 years ago
c.conn = conn
go c.readLoop()
4 years ago
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 {
select {
4 years ago
case req := <-c.outbox:
payload, err := json.Marshal(req)
if err != nil {
c.Error("unable to marshal a request: %v", err)
break
}
w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
c.Error("unable to get a websocket frame writer: %v", err)
break
}
if _, err := w.Write(payload); err != nil {
4 years ago
c.Error("failed to write payload of length %d: %v", len(payload), err)
break
}
c.Child("sent-frame").Info(string(payload))
4 years ago
if err := w.Close(); err != nil {
c.Error("failed to close websocket write frame: %v", err)
}
case <-ctx.Done():
c.Info("parent context done, sending close message")
msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if err := conn.WriteMessage(websocket.CloseMessage, msg); err != nil {
c.Error("failed to write close message: %v", err)
}
c.Info("closing connection")
if err := conn.Close(); err != nil {
c.Error("failed to close connection: %v", err)
}
c.Info("connection closed")
4 years ago
c.conn = nil
return
}
4 years ago
}
}
4 years ago
func (c *client) send(v wire.Value) {
4 years ago
c.lastSeq++
c.outbox <- wire.NewRequest(c.lastSeq, v)
4 years ago
}
func (c *client) readLoop() {
for {
_, r, err := c.conn.NextReader()
if err != nil {
return
}
b, err := ioutil.ReadAll(r)
if err != nil {
return
}
c.Log.Child("received-frame").Info(string(b))
}
}