Compare commits

...

2 Commits

Author SHA1 Message Date
dingjun 103eb1ec6a change id to int 4 years ago
dingjun b3cbbe2228 add CallWithHeader 4 years ago

@ -47,8 +47,12 @@ func (h *HTTPTransport) nextID() uint64 {
return h.nextid return h.nextid
} }
// Call call a remote method
func (h *HTTPTransport) Call(method string, args interface{}, reply interface{}) error { func (h *HTTPTransport) Call(method string, args interface{}, reply interface{}) error {
return h.CallWithHeader(method, args, reply, nil)
}
// Call call a remote method
func (h *HTTPTransport) CallWithHeader(method string, args interface{}, reply interface{}, header http.Header) error {
if args == nil { if args == nil {
args = []string{} args = []string{}
} }
@ -57,7 +61,7 @@ func (h *HTTPTransport) Call(method string, args interface{}, reply interface{})
Version: "2.0", Version: "2.0",
Method: method, Method: method,
Params: args, Params: args,
ID: fmt.Sprintf("%d", h.nextID()), ID: h.nextID(),
} }
data, err := json.Marshal(r) data, err := json.Marshal(r)
@ -74,6 +78,14 @@ func (h *HTTPTransport) Call(method string, args interface{}, reply interface{})
return err return err
} }
if header != nil {
for k, v := range header {
for _, v1 := range v {
req.Header.Add(k, v1)
}
}
}
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
resp, err := h.Client.Do(req) resp, err := h.Client.Do(req)

@ -22,14 +22,14 @@ type request struct {
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Method string `json:"method"` Method string `json:"method"`
Params interface{} `json:"params"` Params interface{} `json:"params"`
ID string `json:"id"` ID uint64 `json:"id"`
} }
type response struct { type response struct {
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Result json.RawMessage `json:"result"` Result json.RawMessage `json:"result"`
Error *Error `json:"error"` Error *Error `json:"error"`
ID string `json:"id"` ID uint64 `json:"id"`
Method string `json:"method"` Method string `json:"method"`
Params json.RawMessage `json:"params"` Params json.RawMessage `json:"params"`
} }

@ -17,7 +17,7 @@ type WebsocketTransport struct {
Conn *websocket.Conn Conn *websocket.Conn
URL string URL string
Mu *sync.Mutex Mu *sync.Mutex
inflight map[string]*inflightReq inflight map[uint64]*inflightReq
nextid uint64 nextid uint64
err error err error
ctx context.Context ctx context.Context
@ -52,7 +52,7 @@ func NewWebsocketTransport(ctx context.Context, uri string) (Transport, error) {
w := &WebsocketTransport{ w := &WebsocketTransport{
Conn: conn, Conn: conn,
URL: uri, URL: uri,
inflight: make(map[string]*inflightReq), inflight: make(map[uint64]*inflightReq),
subscribes: make(map[string][]*inflightReq), subscribes: make(map[string][]*inflightReq),
Mu: new(sync.Mutex), Mu: new(sync.Mutex),
} }
@ -96,7 +96,7 @@ func (h *WebsocketTransport) readloop() {
return return
} }
if res.ID != "" { if res.ID != 0 {
// response // response
h.Mu.Lock() h.Mu.Lock()
@ -173,7 +173,7 @@ func (h *WebsocketTransport) Call(method string, args interface{}, reply interfa
return h.err return h.err
} }
id := fmt.Sprintf("%d", h.nextID()) id := h.nextID()
req := request{ req := request{
ID: id, ID: id,
Version: "2.0", Version: "2.0",
@ -191,7 +191,7 @@ func (h *WebsocketTransport) Call(method string, args interface{}, reply interfa
h.Mu.Lock() h.Mu.Lock()
h.inflight[id] = &inflightReq{ h.inflight[id] = &inflightReq{
id: id, id: fmt.Sprintf("%d", id),
ch: ch, ch: ch,
errch: errch, errch: errch,
} }

Loading…
Cancel
Save