mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
rpc: improve Notifier.send
This commit is contained in:
parent
4180b5d442
commit
412aec48b8
2 changed files with 18 additions and 14 deletions
11
rpc/json.go
11
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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue