diff --git a/event_aux.go b/event_aux.go index e0bfa37..3cac60f 100644 --- a/event_aux.go +++ b/event_aux.go @@ -106,7 +106,7 @@ func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) { } // MarshalJSON() returns the JSON byte encoding of the event, as in NIP-01. -func (evt *Event) MarshalJSON() ([]byte, error) { +func (evt Event) MarshalJSON() ([]byte, error) { dst := make([]byte, 0) dst = append(dst, '{') dst = append(dst, []byte(fmt.Sprintf("\"id\":\"%s\",\"pubkey\":\"%s\",\"created_at\":%d,\"kind\":%d,\"tags\":", diff --git a/relay.go b/relay.go index e632e6c..dd8ca41 100644 --- a/relay.go +++ b/relay.go @@ -229,15 +229,7 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status { defer r.okCallbacks.Delete(event.ID) // publish event - message := []byte("[\"EVENT\",") - if m, e := event.MarshalJSON(); e == nil { - message = append(message, m...) - message = append(message, ']') - } else { - return status - } - - if err := r.Connection.WriteMessage(websocket.TextMessage, message); err != nil { + if err := r.Connection.WriteJSON([]interface{}{"EVENT", event}); err != nil { return status } diff --git a/tags.go b/tags.go index a6e8f68..2dd5ed8 100644 --- a/tags.go +++ b/tags.go @@ -12,15 +12,10 @@ type Tag []string // StartsWith checks if a tag contains a prefix. // for example, -// // ["p", "abcdef...", "wss://relay.com"] -// // would match against -// // ["p", "abcdef..."] -// // or even -// // ["p", "abcdef...", "wss://"] func (tag Tag) StartsWith(prefix []string) bool { prefixLen := len(prefix) @@ -160,7 +155,8 @@ func (tag Tag) marshalTo(dst []byte) []byte { return dst } -// Marshal Tags. Used for Serialization so string escaping should be as in RFC8259. +// MarshalTo appends the JSON encoded byte of Tags as [][]string to dst. +// String escaping is as described in RFC8259. func (tags Tags) marshalTo(dst []byte) []byte { dst = append(dst, '[') for i, tag := range tags {