mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-18 01:56:30 +01:00
* fix: bump ping timeout for poor connections * fix: always close relay on ping failure * fix: add multiple ping attempts before closing relay connection * fix: only 3 ping attempts * fix: pool check for relay context done * fix: include relay URL in debug log * fix: do not return after closing relay on max ping attempts * fix: compile error
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package nostr
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
ws "github.com/coder/websocket"
|
|
)
|
|
|
|
// Connection represents a websocket connection to a Nostr relay.
|
|
type Connection struct {
|
|
conn *ws.Conn
|
|
}
|
|
|
|
// NewConnection creates a new websocket connection to a Nostr relay.
|
|
func NewConnection(ctx context.Context, url string, requestHeader http.Header, tlsConfig *tls.Config) (*Connection, error) {
|
|
c, _, err := ws.Dial(ctx, url, getConnectionOptions(requestHeader, tlsConfig))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c.SetReadLimit(2 << 24) // 33MB
|
|
|
|
return &Connection{
|
|
conn: c,
|
|
}, nil
|
|
}
|
|
|
|
// WriteMessage writes arbitrary bytes to the websocket connection.
|
|
func (c *Connection) WriteMessage(ctx context.Context, data []byte) error {
|
|
if err := c.conn.Write(ctx, ws.MessageText, data); err != nil {
|
|
return fmt.Errorf("failed to write message: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ReadMessage reads arbitrary bytes from the websocket connection into the provided buffer.
|
|
func (c *Connection) ReadMessage(ctx context.Context, buf io.Writer) error {
|
|
_, reader, err := c.conn.Reader(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get reader: %w", err)
|
|
}
|
|
if _, err := io.Copy(buf, reader); err != nil {
|
|
return fmt.Errorf("failed to read message: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close closes the websocket connection.
|
|
func (c *Connection) Close() error {
|
|
return c.conn.Close(ws.StatusNormalClosure, "")
|
|
}
|
|
|
|
// Ping sends a ping message to the websocket connection.
|
|
func (c *Connection) Ping(ctx context.Context) error {
|
|
ctx, cancel := context.WithTimeoutCause(ctx, time.Millisecond*10000, errors.New("ping took too long"))
|
|
defer cancel()
|
|
return c.conn.Ping(ctx)
|
|
}
|