mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
subscription ids as sequential numbers and sub.GetID()
This commit is contained in:
parent
cef5892bce
commit
fdc99d61b6
18
relay.go
18
relay.go
@ -2,8 +2,6 @@ package nostr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -23,6 +21,8 @@ const (
|
||||
PublishStatusSucceeded Status = 1
|
||||
)
|
||||
|
||||
var subscriptionIdCounter = 0
|
||||
|
||||
func (s Status) String() string {
|
||||
switch s {
|
||||
case PublishStatusSent:
|
||||
@ -41,7 +41,7 @@ type Relay struct {
|
||||
RequestHeader http.Header // e.g. for origin header
|
||||
|
||||
Connection *Connection
|
||||
subscriptions s.MapOf[string, *Subscription]
|
||||
subscriptions s.MapOf[int, *Subscription]
|
||||
|
||||
Challenges chan string // NIP-42 Challenges
|
||||
Notices chan string
|
||||
@ -149,7 +149,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
continue
|
||||
}
|
||||
|
||||
var channel string
|
||||
var channel int
|
||||
json.Unmarshal(jsonMessage[1], &channel)
|
||||
if subscription, ok := r.subscriptions.Load(channel); ok {
|
||||
var event Event
|
||||
@ -181,7 +181,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
if len(jsonMessage) < 2 {
|
||||
continue
|
||||
}
|
||||
var channel string
|
||||
var channel int
|
||||
json.Unmarshal(jsonMessage[1], &channel)
|
||||
if subscription, ok := r.subscriptions.Load(channel); ok {
|
||||
subscription.emitEose.Do(func() {
|
||||
@ -386,13 +386,9 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) []*Event {
|
||||
}
|
||||
|
||||
func (r *Relay) PrepareSubscription() *Subscription {
|
||||
random := make([]byte, 7)
|
||||
rand.Read(random)
|
||||
id := hex.EncodeToString(random)
|
||||
return r.prepareSubscription(id)
|
||||
}
|
||||
id := subscriptionIdCounter
|
||||
subscriptionIdCounter++
|
||||
|
||||
func (r *Relay) prepareSubscription(id string) *Subscription {
|
||||
sub := &Subscription{
|
||||
Relay: r,
|
||||
conn: r.Connection,
|
||||
|
@ -2,11 +2,12 @@ package nostr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
id string
|
||||
id int
|
||||
conn *Connection
|
||||
mutex sync.Mutex
|
||||
|
||||
@ -25,13 +26,18 @@ type EventMessage struct {
|
||||
Relay string
|
||||
}
|
||||
|
||||
// GetID return the Nostr subscription ID as given to the relay, it will be a sequential number, stringified
|
||||
func (sub *Subscription) GetID() string {
|
||||
return strconv.Itoa(sub.id)
|
||||
}
|
||||
|
||||
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
||||
// Unsub() also closes the channel sub.Events.
|
||||
func (sub *Subscription) Unsub() {
|
||||
sub.mutex.Lock()
|
||||
defer sub.mutex.Unlock()
|
||||
|
||||
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
|
||||
sub.conn.WriteJSON([]interface{}{"CLOSE", strconv.Itoa(sub.id)})
|
||||
if sub.stopped == false && sub.Events != nil {
|
||||
close(sub.Events)
|
||||
}
|
||||
@ -50,7 +56,7 @@ func (sub *Subscription) Fire(ctx context.Context) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
sub.Context = ctx
|
||||
|
||||
message := []interface{}{"REQ", sub.id}
|
||||
message := []interface{}{"REQ", strconv.Itoa(sub.id)}
|
||||
for _, filter := range sub.Filters {
|
||||
message = append(message, filter)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user