go-nostr/subscription.go

37 lines
565 B
Go
Raw Permalink Normal View History

2022-01-02 08:44:18 -03:00
package nostr
2021-02-20 17:44:05 -03:00
type Subscription struct {
id string
conn *Connection
2021-02-20 17:44:05 -03:00
filters Filters
Events chan Event
stopped bool
}
type EventMessage struct {
2022-01-02 08:44:18 -03:00
Event Event
Relay string
2021-02-20 17:44:05 -03:00
}
func (sub Subscription) Unsub() {
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
2021-02-20 17:44:05 -03:00
sub.stopped = true
if sub.Events != nil {
close(sub.Events)
2021-02-20 17:44:05 -03:00
}
}
func (sub *Subscription) Sub(filters Filters) {
sub.filters = filters
message := []interface{}{"REQ", sub.id}
for _, filter := range sub.filters {
message = append(message, filter)
}
sub.conn.WriteJSON(message)
2021-02-20 17:44:05 -03:00
}