Update WebSocket implementation to use Gorilla WebSocket library
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled

- Replaced the existing `github.com/coder/websocket` package with `github.com/gorilla/websocket` for improved functionality and compatibility.
- Adjusted WebSocket connection handling, including message reading and writing, to align with the new library's API.
- Enhanced error handling and logging for WebSocket operations.
- Bumped version to v0.20.0 to reflect the changes made.
This commit is contained in:
2025-10-30 15:20:39 +00:00
parent 4f97cb9a42
commit 88ebf6eccc
9 changed files with 158 additions and 146 deletions

View File

@@ -5,32 +5,21 @@ package ws
import (
"crypto/tls"
"net/http"
"net/textproto"
"time"
ws "github.com/coder/websocket"
"github.com/gorilla/websocket"
)
var defaultConnectionOptions = &ws.DialOptions{
CompressionMode: ws.CompressionContextTakeover,
HTTPHeader: http.Header{
textproto.CanonicalMIMEHeaderKey("User-Agent"): {"github.com/nbd-wtf/go-nostr"},
},
}
func getConnectionOptions(
requestHeader http.Header, tlsConfig *tls.Config,
) *ws.DialOptions {
if requestHeader == nil && tlsConfig == nil {
return defaultConnectionOptions
}
return &ws.DialOptions{
HTTPHeader: requestHeader,
CompressionMode: ws.CompressionContextTakeover,
HTTPClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
},
) *websocket.Dialer {
dialer := &websocket.Dialer{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
TLSClientConfig: tlsConfig,
HandshakeTimeout: 10 * time.Second,
}
// Headers are passed directly to DialContext, not set on Dialer
// The User-Agent header will be set when calling DialContext if not present
return dialer
}