2021-03-17 09:20:33 +00:00
|
|
|
import
|
2021-06-09 14:59:52 +00:00
|
|
|
std/[strtabs, tables],
|
|
|
|
chronos,
|
|
|
|
../client
|
|
|
|
|
|
|
|
export client
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
const newsUseChronos = true
|
|
|
|
include news
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcWebSocketClient* = ref object of RpcClient
|
|
|
|
transport*: WebSocket
|
|
|
|
uri*: string
|
|
|
|
loop*: Future[void]
|
|
|
|
|
2021-03-17 09:20:33 +00:00
|
|
|
proc new*(T: type RpcWebSocketClient): T =
|
|
|
|
T()
|
|
|
|
|
2020-12-21 11:45:07 +00:00
|
|
|
proc newRpcWebSocketClient*: RpcWebSocketClient =
|
|
|
|
## Creates a new client instance.
|
2021-03-17 09:20:33 +00:00
|
|
|
RpcWebSocketClient.new()
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
method call*(self: RpcWebSocketClient, name: string,
|
2021-06-09 14:59:52 +00:00
|
|
|
params: JsonNode): Future[Response] {.
|
|
|
|
async, gcsafe, raises: [Defect, CatchableError].} =
|
2020-12-21 11:45:07 +00:00
|
|
|
## Remotely calls the specified RPC method.
|
|
|
|
let id = self.getNextId()
|
2021-03-17 09:20:33 +00:00
|
|
|
var value = $rpcCallNode(name, params, id) & "\r\n"
|
2020-12-21 11:45:07 +00:00
|
|
|
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)
|
2021-03-17 09:20:33 +00:00
|
|
|
return await newFut
|
2020-12-21 11:45:07 +00:00
|
|
|
|
|
|
|
proc processData(client: RpcWebSocketClient) {.async.} =
|
2021-03-17 09:20:33 +00:00
|
|
|
var error: ref CatchableError
|
2020-12-21 11:45:07 +00:00
|
|
|
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()
|
|
|
|
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)
|
|
|
|
|
|
|
|
method close*(client: RpcWebSocketClient) {.async.} =
|
2021-06-09 14:59:52 +00:00
|
|
|
await client.loop.cancelAndWait()
|
2020-12-21 11:45:07 +00:00
|
|
|
if not client.transport.isNil:
|
2021-06-09 14:59:52 +00:00
|
|
|
client.transport.close()
|
|
|
|
client.transport = nil
|
|
|
|
|