mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-06 21:19:28 +02:00
fix relay .Connect() and .Sub()
This commit is contained in:
parent
37235a1394
commit
2b8807d699
20
relay.go
20
relay.go
@ -61,11 +61,13 @@ func (r *Relay) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conn := NewConnection(socket)
|
conn := NewConnection(socket)
|
||||||
|
r.Connection = conn
|
||||||
|
|
||||||
|
go func() {
|
||||||
for {
|
for {
|
||||||
typ, message, err := conn.socket.ReadMessage()
|
typ, message, err := conn.socket.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("read error: %w", err)
|
continue
|
||||||
}
|
}
|
||||||
if typ == websocket.PingMessage {
|
if typ == websocket.PingMessage {
|
||||||
conn.WriteMessage(websocket.PongMessage, nil)
|
conn.WriteMessage(websocket.PongMessage, nil)
|
||||||
@ -154,6 +156,9 @@ func (r *Relay) Connect() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Relay) Publish(event Event) chan Status {
|
func (r Relay) Publish(event Event) chan Status {
|
||||||
@ -194,6 +199,10 @@ func (r Relay) Publish(event Event) chan Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Relay) Subscribe(filters Filters) *Subscription {
|
func (r *Relay) Subscribe(filters Filters) *Subscription {
|
||||||
|
if r.Connection == nil {
|
||||||
|
panic(fmt.Errorf("must call .Connect() first before calling .Subscribe()"))
|
||||||
|
}
|
||||||
|
|
||||||
random := make([]byte, 7)
|
random := make([]byte, 7)
|
||||||
rand.Read(random)
|
rand.Read(random)
|
||||||
id := hex.EncodeToString(random)
|
id := hex.EncodeToString(random)
|
||||||
@ -201,10 +210,13 @@ func (r *Relay) Subscribe(filters Filters) *Subscription {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Relay) subscribe(id string, filters Filters) *Subscription {
|
func (r *Relay) subscribe(id string, filters Filters) *Subscription {
|
||||||
sub := Subscription{}
|
sub := Subscription{
|
||||||
sub.id = id
|
conn: r.Connection,
|
||||||
|
id: id,
|
||||||
|
Events: make(chan Event),
|
||||||
|
EndOfStoredEvents: make(chan struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
sub.Events = make(chan Event)
|
|
||||||
r.subscriptions.Store(sub.id, &sub)
|
r.subscriptions.Store(sub.id, &sub)
|
||||||
|
|
||||||
sub.Sub(filters)
|
sub.Sub(filters)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user