mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-06 21:21:36 +02:00
use coder/websocket for everything, get rid of gobwas.
supposedly it is faster, and anyway it's better to use it since we're already using it for wasm/js. (previously named nhooyr/websocket).
This commit is contained in:
18
relay.go
18
relay.go
@@ -23,7 +23,7 @@ type Relay struct {
|
||||
closeMutex sync.Mutex
|
||||
|
||||
URL string
|
||||
RequestHeader http.Header // e.g. for origin header
|
||||
requestHeader http.Header // e.g. for origin header
|
||||
|
||||
Connection *Connection
|
||||
Subscriptions *xsync.MapOf[int64, *Subscription]
|
||||
@@ -60,7 +60,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay {
|
||||
okCallbacks: xsync.NewMapOf[string, func(bool, string)](),
|
||||
writeQueue: make(chan writeRequest),
|
||||
subscriptionChannelCloseQueue: make(chan *Subscription),
|
||||
RequestHeader: make(http.Header, 1),
|
||||
requestHeader: nil,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -88,6 +88,7 @@ type RelayOption interface {
|
||||
var (
|
||||
_ RelayOption = (WithNoticeHandler)(nil)
|
||||
_ RelayOption = (WithCustomHandler)(nil)
|
||||
_ RelayOption = (WithRequestHeader)(nil)
|
||||
)
|
||||
|
||||
// WithNoticeHandler just takes notices and is expected to do something with them.
|
||||
@@ -106,6 +107,13 @@ func (ch WithCustomHandler) ApplyRelayOption(r *Relay) {
|
||||
r.customHandler = ch
|
||||
}
|
||||
|
||||
// WithRequestHeader sets the HTTP request header of the websocket preflight request.
|
||||
type WithRequestHeader http.Header
|
||||
|
||||
func (ch WithRequestHeader) ApplyRelayOption(r *Relay) {
|
||||
r.requestHeader = http.Header(ch)
|
||||
}
|
||||
|
||||
// String just returns the relay URL.
|
||||
func (r *Relay) String() string {
|
||||
return r.URL
|
||||
@@ -146,11 +154,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
if r.RequestHeader.Get("User-Agent") == "" {
|
||||
r.RequestHeader.Set("User-Agent", "github.com/nbd-wtf/go-nostr")
|
||||
}
|
||||
|
||||
conn, err := NewConnection(ctx, r.URL, r.RequestHeader, tlsConfig)
|
||||
conn, err := NewConnection(ctx, r.URL, r.requestHeader, tlsConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user