diff options
author | Bas van Kervel <basvankervel@gmail.com> | 2017-04-06 14:56:41 +0800 |
---|---|---|
committer | Bas van Kervel <basvankervel@gmail.com> | 2017-04-25 17:13:22 +0800 |
commit | 37e3f561f15cbedf10c01847e58a079f9b86bf6f (patch) | |
tree | d7cdc3e8a5b74261a3359f6029e927cca0fc738b /rpc/json.go | |
parent | ba3bcd16a6d99bc0e58516556df8e96b730c2d60 (diff) | |
download | dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar.gz dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar.bz2 dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar.lz dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar.xz dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.tar.zst dexon-37e3f561f15cbedf10c01847e58a079f9b86bf6f.zip |
rpc: support subscriptions under custom namespaces
Diffstat (limited to 'rpc/json.go')
-rw-r--r-- | rpc/json.go | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/rpc/json.go b/rpc/json.go index c777fab6e..2e7fd599e 100644 --- a/rpc/json.go +++ b/rpc/json.go @@ -30,11 +30,11 @@ import ( ) const ( - jsonrpcVersion = "2.0" - serviceMethodSeparator = "_" - subscribeMethod = "eth_subscribe" - unsubscribeMethod = "eth_unsubscribe" - notificationMethod = "eth_subscription" + jsonrpcVersion = "2.0" + serviceMethodSeparator = "_" + subscribeMethodSuffix = "_subscribe" + unsubscribeMethodSuffix = "_unsubscribe" + notificationMethodSuffix = "_subscription" ) type jsonRequest struct { @@ -164,7 +164,7 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) { } // subscribe are special, they will always use `subscribeMethod` as first param in the payload - if in.Method == subscribeMethod { + if strings.HasSuffix(in.Method, subscribeMethodSuffix) { reqs := []rpcRequest{{id: &in.Id, isPubSub: true}} if len(in.Payload) > 0 { // first param must be subscription name @@ -174,17 +174,16 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) { return nil, false, &invalidRequestError{"Unable to parse subscription request"} } - // all subscriptions are made on the eth service - reqs[0].service, reqs[0].method = "eth", subscribeMethod[0] + reqs[0].service, reqs[0].method = strings.TrimSuffix(in.Method, subscribeMethodSuffix), subscribeMethod[0] reqs[0].params = in.Payload return reqs, false, nil } return nil, false, &invalidRequestError{"Unable to parse subscription request"} } - if in.Method == unsubscribeMethod { + if strings.HasSuffix(in.Method, unsubscribeMethodSuffix) { return []rpcRequest{{id: &in.Id, isPubSub: true, - method: unsubscribeMethod, params: in.Payload}}, false, nil + method: in.Method, params: in.Payload}}, false, nil } elems := strings.Split(in.Method, serviceMethodSeparator) @@ -216,8 +215,8 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) id := &in[i].Id - // subscribe are special, they will always use `subscribeMethod` as first param in the payload - if r.Method == subscribeMethod { + // subscribe are special, they will always use `subscriptionMethod` as first param in the payload + if strings.HasSuffix(r.Method, subscribeMethodSuffix) { requests[i] = rpcRequest{id: id, isPubSub: true} if len(r.Payload) > 0 { // first param must be subscription name @@ -227,8 +226,7 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) return nil, false, &invalidRequestError{"Unable to parse subscription request"} } - // all subscriptions are made on the eth service - requests[i].service, requests[i].method = "eth", subscribeMethod[0] + requests[i].service, requests[i].method = strings.TrimSuffix(r.Method, subscribeMethodSuffix), subscribeMethod[0] requests[i].params = r.Payload continue } @@ -236,8 +234,8 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"} } - if r.Method == unsubscribeMethod { - requests[i] = rpcRequest{id: id, isPubSub: true, method: unsubscribeMethod, params: r.Payload} + if strings.HasSuffix(r.Method, unsubscribeMethodSuffix) { + requests[i] = rpcRequest{id: id, isPubSub: true, method: r.Method, params: r.Payload} continue } @@ -325,13 +323,13 @@ func (c *jsonCodec) CreateErrorResponseWithInfo(id interface{}, err Error, info } // CreateNotification will create a JSON-RPC notification with the given subscription id and event as params. -func (c *jsonCodec) CreateNotification(subid string, event interface{}) interface{} { +func (c *jsonCodec) CreateNotification(subid, namespace string, event interface{}) interface{} { if isHexNum(reflect.TypeOf(event)) { - return &jsonNotification{Version: jsonrpcVersion, Method: notificationMethod, + return &jsonNotification{Version: jsonrpcVersion, Method: namespace + notificationMethodSuffix, Params: jsonSubscription{Subscription: subid, Result: fmt.Sprintf(`%#x`, event)}} } - return &jsonNotification{Version: jsonrpcVersion, Method: notificationMethod, + return &jsonNotification{Version: jsonrpcVersion, Method: namespace + notificationMethodSuffix, Params: jsonSubscription{Subscription: subid, Result: event}} } |