-
Notifications
You must be signed in to change notification settings - Fork 28
/
websocketclient.nim
151 lines (128 loc) · 4.1 KB
/
websocketclient.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import
std/[strtabs, tables],
pkg/[chronos, chronicles],
stew/byteutils,
../client, ./config
export client
# TODO needs fixes in news
# {.push raises: [Defect].}
logScope:
topics = "JSONRPC-WS-CLIENT"
when useNews:
const newsUseChronos = true
include pkg/news
type
RpcWebSocketClient* = ref object of RpcClient
transport*: WebSocket
uri*: string
loop*: Future[void]
getHeaders*: GetJsonRpcRequestHeaders
else:
import std/[uri, strutils]
import pkg/websock/[websock, extensions/compression/deflate]
type
RpcWebSocketClient* = ref object of RpcClient
transport*: WSSession
uri*: Uri
loop*: Future[void]
getHeaders*: GetJsonRpcRequestHeaders
proc new*(
T: type RpcWebSocketClient, getHeaders: GetJsonRpcRequestHeaders = nil): T =
T(getHeaders: getHeaders)
proc newRpcWebSocketClient*(
getHeaders: GetJsonRpcRequestHeaders = nil): RpcWebSocketClient =
## Creates a new client instance.
RpcWebSocketClient.new(getHeaders)
method call*(self: RpcWebSocketClient, name: string,
params: JsonNode): Future[Response] {.async, gcsafe.} =
## Remotely calls the specified RPC method.
let id = self.getNextId()
var value = $rpcCallNode(name, params, id) & "\r\n"
if self.transport.isNil:
raise newException(ValueError,
"Transport is not initialised (missing a call to connect?)")
# completed by processMessage.
var newFut = newFuture[Response]()
# add to awaiting responses
self.awaiting[id] = newFut
await self.transport.send(value)
return await newFut
proc processData(client: RpcWebSocketClient) {.async.} =
var error: ref CatchableError
when useNews:
try:
while true:
var value = await client.transport.receiveString()
if value == "":
# transmission ends
break
client.processMessage(value)
except CatchableError as e:
error = e
client.transport.close()
else:
let ws = client.transport
try:
while ws.readyState != ReadyState.Closed:
var value = await ws.recvMsg()
if value.len == 0:
# transmission ends
break
client.processMessage(string.fromBytes(value))
except CatchableError as e:
error = e
await client.transport.close()
client.transport = nil
if client.awaiting.len != 0:
if error.isNil:
error = newException(IOError, "Transport was closed while waiting for response")
for k, v in client.awaiting:
v.fail(error)
client.awaiting.clear()
if not client.onDisconnect.isNil:
client.onDisconnect()
when useNews:
proc connect*(
client: RpcWebSocketClient,
uri: string,
headers: StringTableRef = nil,
compression = false) {.async.} =
if compression:
warn "compression is not supported with the news back-end"
var headers = headers
if headers.isNil:
headers = newStringTable({"Origin": "http://localhost"})
elif "Origin" notin headers:
# TODO: This is a hack, because the table might be case sensitive. Ideally strtabs module has
# to be extended with case insensitive accessors.
headers["Origin"] = "http://localhost"
if not isNil(client.getHeaders):
for header in client.getHeaders():
headers[header[0]] = header[1]
client.transport = await newWebSocket(uri, headers)
client.uri = uri
client.loop = processData(client)
else:
proc connect*(
client: RpcWebSocketClient, uri: string,
compression = false,
flags: set[TLSFlags] = {NoVerifyHost, NoVerifyServerName}) {.async.} =
var ext: seq[ExtFactory] = if compression: @[deflateFactory()]
else: @[]
let uri = parseUri(uri)
let ws = await WebSocket.connect(
uri=uri,
factories=ext,
flags=flags
)
client.transport = ws
client.uri = uri
client.loop = processData(client)
method close*(client: RpcWebSocketClient) {.async.} =
await client.loop.cancelAndWait()
if not client.transport.isNil:
when useNews:
client.transport.close()
else:
await client.transport.close()
client.transport = nil