Use github.com/nhooyr/websocket instead of github.com/gobwas/ws

This commit is contained in:
Trevor Slocum 2024-07-18 12:41:46 -07:00
parent 23ab2deae4
commit 8bb53ef135
3 changed files with 22 additions and 42 deletions

4
go.mod
View file

@ -5,13 +5,13 @@ go 1.17
require (
code.rocket9labs.com/tslocum/tabula v0.0.0-20240703054156-ce0b448f0999
github.com/alexedwards/argon2id v1.0.0
github.com/gobwas/ws v1.4.0
github.com/gorilla/mux v1.8.1
github.com/jackc/pgx/v5 v5.6.0
github.com/jlouis/glicko2 v1.0.0
github.com/leonelquinteros/gotext v1.6.1
github.com/matcornic/hermes/v2 v2.1.0
golang.org/x/text v0.16.0
nhooyr.io/websocket v1.8.11
)
require (
@ -21,8 +21,6 @@ require (
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/PuerkitoBio/goquery v1.9.2 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/huandu/xstrings v1.5.0 // indirect

9
go.sum
View file

@ -25,12 +25,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df/go.mod h1:GJr+FCSXshIwgHBtLglIg9M2l2kQSi6QjVAngtzI08Y=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -134,7 +128,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -170,3 +163,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=

View file

@ -2,21 +2,23 @@ package server
import (
"bytes"
"context"
"log"
"net"
"net/http"
"sync"
"time"
"code.rocket9labs.com/tslocum/bgammon"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"nhooyr.io/websocket"
)
var acceptOptions = &websocket.AcceptOptions{
InsecureSkipVerify: true,
}
var _ bgammon.Client = &webSocketClient{}
type webSocketClient struct {
conn net.Conn
conn *websocket.Conn
events chan []byte
commands chan<- []byte
terminated bool
@ -25,7 +27,7 @@ type webSocketClient struct {
}
func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- []byte, events chan []byte, verbose bool) *webSocketClient {
conn, _, _, err := ws.UpgradeHTTP(r, w)
conn, err := websocket.Accept(w, r, acceptOptions)
if err != nil {
return nil
}
@ -61,48 +63,33 @@ func (c *webSocketClient) Write(message []byte) {
}
func (c *webSocketClient) readCommands() {
setTimeout := func() {
err := c.conn.SetReadDeadline(time.Now().Add(clientTimeout))
if err != nil {
c.Terminate(err.Error())
return
}
}
var ctx context.Context
for {
if c.terminated {
return
}
setTimeout()
msg, op, err := wsutil.ReadClientData(c.conn)
ctx, _ = context.WithTimeout(context.Background(), clientTimeout)
msgType, msgContent, err := c.conn.Read(ctx)
if err != nil {
c.Terminate(err.Error())
return
} else if op != ws.OpText {
} else if msgType != websocket.MessageText {
continue
}
buf := make([]byte, len(msg))
copy(buf, msg)
buf := make([]byte, len(msgContent))
copy(buf, msgContent)
c.commands <- buf
if c.verbose {
logClientRead(msg)
logClientRead(msgContent)
}
}
}
func (c *webSocketClient) writeEvents(closeWrite chan struct{}) {
setTimeout := func() {
err := c.conn.SetWriteDeadline(time.Now().Add(clientTimeout))
if err != nil {
c.Terminate(err.Error())
return
}
}
setTimeout()
var ctx context.Context
var event []byte
for {
select {
@ -123,8 +110,8 @@ func (c *webSocketClient) writeEvents(closeWrite chan struct{}) {
continue
}
setTimeout()
err := wsutil.WriteServerMessage(c.conn, ws.OpText, event)
ctx, _ = context.WithTimeout(context.Background(), clientTimeout)
err := c.conn.Write(ctx, websocket.MessageText, event)
if err != nil {
c.Terminate(err.Error())
c.wgEvents.Done()
@ -143,7 +130,7 @@ func (c *webSocketClient) Terminate(reason string) {
return
}
c.terminated = true
c.conn.Close()
c.conn.CloseNow()
}
func (c *webSocketClient) Terminated() bool {