nim-json-rpc/json_rpc/clients/websocketclient.nim

75 lines
2.2 KiB
Nim
Raw Normal View History

2019-11-22 14:37:54 +02:00
import ../client, chronos, tables, json, strtabs
const newsUseChronos = true
include news
type
RpcWebSocketClient* = ref object of RpcClient
transport*: WebSocket
uri*: string
loop*: Future[void]
proc newRpcWebSocketClient*: RpcWebSocketClient =
## Creates a new client instance.
new result
result.initRpcClient()
method call*(self: RpcWebSocketClient, name: string,
2020-03-17 22:05:42 +02:00
params: JsonNode): Future[Response] {.async.} =
## Remotely calls the specified RPC method.
let id = self.getNextId()
var value = $rpcCallNode(name, params, id) & "\c\l"
if self.transport.isNil:
raise newException(ValueError,
"Transport is not initialised (missing a call to connect?)")
# echo "Sent msg: ", value
# completed by processMessage.
var newFut = newFuture[Response]()
# add to awaiting responses
self.awaiting[id] = newFut
await self.transport.send(value)
result = await newFut
proc processData(client: RpcWebSocketClient) {.async.} =
2019-11-22 14:37:54 +02:00
var error: ref Exception
try:
while true:
2020-05-08 16:13:00 +03:00
var value = await client.transport.receiveString()
if value == "":
# transmission ends
break
client.processMessage(value)
2019-11-22 14:37:54 +02:00
except CatchableError as e:
error = e
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()
proc connect*(client: RpcWebSocketClient, uri: string, headers: StringTableRef = nil) {.async.} =
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"
client.transport = await newWebSocket(uri, headers)
client.uri = uri
client.loop = processData(client)
2019-06-17 19:56:19 +03:00
method close*(client: RpcWebSocketClient) {.async.} =
2019-11-22 14:37:54 +02:00
if not client.transport.isNil:
client.loop.cancel()