mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-18 11:32:25 +02:00
support for EOSE and OK messages on relay/subscription.
This commit is contained in:
32
relay.go
32
relay.go
@@ -40,6 +40,7 @@ type Relay struct {
|
|||||||
subscriptions s.MapOf[string, *Subscription]
|
subscriptions s.MapOf[string, *Subscription]
|
||||||
|
|
||||||
Notices chan string
|
Notices chan string
|
||||||
|
statusChans s.MapOf[string, chan Status]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRelay(url string) *Relay {
|
func NewRelay(url string) *Relay {
|
||||||
@@ -124,6 +125,33 @@ func (r *Relay) Connect() error {
|
|||||||
subscription.Events <- event
|
subscription.Events <- event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case "EOSE":
|
||||||
|
if len(jsonMessage) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var channel string
|
||||||
|
json.Unmarshal(jsonMessage[1], &channel)
|
||||||
|
if subscription, ok := r.subscriptions.Load(channel); ok {
|
||||||
|
subscription.EndOfStoredEvents <- struct{}{}
|
||||||
|
}
|
||||||
|
case "OK":
|
||||||
|
if len(jsonMessage) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
eventId string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
json.Unmarshal(jsonMessage[1], &eventId)
|
||||||
|
json.Unmarshal(jsonMessage[2], &ok)
|
||||||
|
|
||||||
|
if statusChan, ok := r.statusChans.Load(eventId); ok {
|
||||||
|
if ok {
|
||||||
|
statusChan <- PublishStatusSucceeded
|
||||||
|
} else {
|
||||||
|
statusChan <- PublishStatusFailed
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,6 +160,10 @@ func (r Relay) Publish(event Event) chan Status {
|
|||||||
statusChan := make(chan Status)
|
statusChan := make(chan Status)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
// we keep track of this so the OK message can be used to close it
|
||||||
|
r.statusChans.Store(event.ID, statusChan)
|
||||||
|
defer r.statusChans.Delete(event.ID)
|
||||||
|
|
||||||
err := r.Connection.WriteJSON([]interface{}{"EVENT", event})
|
err := r.Connection.WriteJSON([]interface{}{"EVENT", event})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
statusChan <- PublishStatusFailed
|
statusChan <- PublishStatusFailed
|
||||||
|
@@ -6,6 +6,7 @@ type Subscription struct {
|
|||||||
|
|
||||||
filters Filters
|
filters Filters
|
||||||
Events chan Event
|
Events chan Event
|
||||||
|
EndOfStoredEvents chan struct{}
|
||||||
|
|
||||||
stopped bool
|
stopped bool
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user