|
|
@ -1,6 +1,7 @@
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
@ -16,7 +17,7 @@ type client struct {
|
|
|
|
port int
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *client) run() {
|
|
|
|
func (c *client) run(ctx context.Context) {
|
|
|
|
dialer := websocket.Dialer{
|
|
|
|
dialer := websocket.Dialer{
|
|
|
|
HandshakeTimeout: 3 * time.Second,
|
|
|
|
HandshakeTimeout: 3 * time.Second,
|
|
|
|
ReadBufferSize: 32 * 1024,
|
|
|
|
ReadBufferSize: 32 * 1024,
|
|
|
@ -41,15 +42,32 @@ func (c *client) run() {
|
|
|
|
c.Debug("dial response header: %s = %s", k, strings.Join(vals, ","))
|
|
|
|
c.Debug("dial response header: %s = %s", k, strings.Join(vals, ","))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tick := time.NewTicker(time.Second)
|
|
|
|
|
|
|
|
defer tick.Stop()
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
w, err := conn.NextWriter(websocket.TextMessage)
|
|
|
|
select {
|
|
|
|
if err != nil {
|
|
|
|
case <-tick.C:
|
|
|
|
c.Error("unable to get a websocket frame writer: %v", err)
|
|
|
|
w, err := conn.NextWriter(websocket.TextMessage)
|
|
|
|
break
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
c.Error("unable to get a websocket frame writer: %v", err)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.Write([]byte("hey"))
|
|
|
|
w.Write([]byte("hey"))
|
|
|
|
w.Close()
|
|
|
|
w.Close()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
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")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|