diff --git a/rpc/json.go b/rpc/json.go index 8a3b162cab..051f22076d 100644 --- a/rpc/json.go +++ b/rpc/json.go @@ -46,6 +46,17 @@ type subscriptionResult struct { Result json.RawMessage `json:"result,omitempty"` } +type subscriptionResultEnc struct { + ID string `json:"subscription"` + Result any `json:"result"` +} + +type jsonrpcSubscriptionNotification struct { + Version string `json:"jsonrpc"` + Method string `json:"method"` + Params subscriptionResultEnc `json:"params"` +} + // A value of this type can a JSON-RPC request, notification, successful response or // error response. Which one it is depends on the fields. type jsonrpcMessage struct { diff --git a/rpc/subscription.go b/rpc/subscription.go index 3c89d9e2d2..9cb0727547 100644 --- a/rpc/subscription.go +++ b/rpc/subscription.go @@ -176,23 +176,16 @@ func (n *Notifier) activate() error { return nil } -func (n *Notifier) send(sub *Subscription, data interface{}) error { - params := struct { - ID string `json:"subscription"` - Result interface{} `json:"result,omitempty"` - }{ID: string(sub.ID), Result: data} - ctx := context.Background() - - msg := struct { - Version string `json:"jsonrpc,omitempty"` - Method string `json:"method,omitempty"` - Params interface{} `json:"params,omitempty"` - }{ +func (n *Notifier) send(sub *Subscription, data any) error { + msg := jsonrpcSubscriptionNotification{ Version: vsn, Method: n.namespace + notificationMethodSuffix, - Params: params, + Params: subscriptionResultEnc{ + ID: string(sub.ID), + Result: data, + }, } - return n.h.conn.writeJSON(ctx, msg, false) + return n.h.conn.writeJSON(context.Background(), &msg, false) } // A Subscription is created by a notifier and tied to that notifier. The client can use