@@ -59,6 +59,12 @@ const (
59
59
maxClientSubscriptionBuffer = 20000
60
60
)
61
61
62
+ const (
63
+ httpScheme = "http"
64
+ wsScheme = "ws"
65
+ ipcScheme = "ipc"
66
+ )
67
+
62
68
// BatchElem is an element in a batch request.
63
69
type BatchElem struct {
64
70
Method string
@@ -75,7 +81,7 @@ type BatchElem struct {
75
81
// Client represents a connection to an RPC server.
76
82
type Client struct {
77
83
idgen func () ID // for subscriptions
78
- isHTTP bool
84
+ scheme string // connection type: http, ws or ipc
79
85
services * serviceRegistry
80
86
81
87
idCounter uint32
@@ -111,6 +117,10 @@ type clientConn struct {
111
117
112
118
func (c * Client ) newClientConn (conn ServerCodec ) * clientConn {
113
119
ctx := context .WithValue (context .Background (), clientContextKey {}, c )
120
+ // Http connections have already set the scheme
121
+ if ! c .isHTTP () && c .scheme != "" {
122
+ ctx = context .WithValue (ctx , "scheme" , c .scheme )
123
+ }
114
124
handler := newHandler (ctx , conn , c .idgen , c .services )
115
125
return & clientConn {conn , handler }
116
126
}
@@ -136,7 +146,7 @@ func (op *requestOp) wait(ctx context.Context, c *Client) (*jsonrpcMessage, erro
136
146
select {
137
147
case <- ctx .Done ():
138
148
// Send the timeout to dispatch so it can remove the request IDs.
139
- if ! c .isHTTP {
149
+ if ! c .isHTTP () {
140
150
select {
141
151
case c .reqTimeout <- op :
142
152
case <- c .closing :
@@ -203,10 +213,18 @@ func newClient(initctx context.Context, connect reconnectFunc) (*Client, error)
203
213
}
204
214
205
215
func initClient (conn ServerCodec , idgen func () ID , services * serviceRegistry ) * Client {
206
- _ , isHTTP := conn .(* httpConn )
216
+ scheme := ""
217
+ switch conn .(type ) {
218
+ case * httpConn :
219
+ scheme = httpScheme
220
+ case * websocketCodec :
221
+ scheme = wsScheme
222
+ case * jsonCodec :
223
+ scheme = ipcScheme
224
+ }
207
225
c := & Client {
208
226
idgen : idgen ,
209
- isHTTP : isHTTP ,
227
+ scheme : scheme ,
210
228
services : services ,
211
229
writeConn : conn ,
212
230
close : make (chan struct {}),
@@ -219,7 +237,7 @@ func initClient(conn ServerCodec, idgen func() ID, services *serviceRegistry) *C
219
237
reqSent : make (chan error , 1 ),
220
238
reqTimeout : make (chan * requestOp ),
221
239
}
222
- if ! isHTTP {
240
+ if ! c . isHTTP () {
223
241
go c .dispatch (conn )
224
242
}
225
243
return c
@@ -250,7 +268,7 @@ func (c *Client) SupportedModules() (map[string]string, error) {
250
268
251
269
// Close closes the client, aborting any in-flight requests.
252
270
func (c * Client ) Close () {
253
- if c .isHTTP {
271
+ if c .isHTTP () {
254
272
return
255
273
}
256
274
select {
@@ -264,7 +282,7 @@ func (c *Client) Close() {
264
282
// This method only works for clients using HTTP, it doesn't have
265
283
// any effect for clients using another transport.
266
284
func (c * Client ) SetHeader (key , value string ) {
267
- if ! c .isHTTP {
285
+ if ! c .isHTTP () {
268
286
return
269
287
}
270
288
conn := c .writeConn .(* httpConn )
@@ -298,7 +316,7 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
298
316
}
299
317
op := & requestOp {ids : []json.RawMessage {msg .ID }, resp : make (chan * jsonrpcMessage , 1 )}
300
318
301
- if c .isHTTP {
319
+ if c .isHTTP () {
302
320
err = c .sendHTTP (ctx , op , msg )
303
321
} else {
304
322
err = c .send (ctx , op , msg )
@@ -357,7 +375,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
357
375
}
358
376
359
377
var err error
360
- if c .isHTTP {
378
+ if c .isHTTP () {
361
379
err = c .sendBatchHTTP (ctx , op , msgs )
362
380
} else {
363
381
err = c .send (ctx , op , msgs )
@@ -402,7 +420,7 @@ func (c *Client) Notify(ctx context.Context, method string, args ...interface{})
402
420
}
403
421
msg .ID = nil
404
422
405
- if c .isHTTP {
423
+ if c .isHTTP () {
406
424
return c .sendHTTP (ctx , op , msg )
407
425
}
408
426
return c .send (ctx , op , msg )
@@ -440,7 +458,7 @@ func (c *Client) Subscribe(ctx context.Context, namespace string, channel interf
440
458
if chanVal .IsNil () {
441
459
panic ("channel given to Subscribe must not be nil" )
442
460
}
443
- if c .isHTTP {
461
+ if c .isHTTP () {
444
462
return nil , ErrNotificationsUnsupported
445
463
}
446
464
@@ -642,3 +660,7 @@ func (c *Client) read(codec ServerCodec) {
642
660
c .readOp <- readOp {msgs , batch }
643
661
}
644
662
}
663
+
664
+ func (c * Client ) isHTTP () bool {
665
+ return c .scheme == httpScheme
666
+ }
0 commit comments