From b8a8ca26239cfcbf89ed4c8a6d446a5c79a257fb Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 23 Jun 2021 18:43:02 +0700 Subject: [PATCH] replace news with nim-ws fixes #103 --- json_rpc.nimble | 2 +- json_rpc/clients/websocketclient.nim | 60 +++++++++++++++++----------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/json_rpc.nimble b/json_rpc.nimble index e12fb4b..4b9ae86 100644 --- a/json_rpc.nimble +++ b/json_rpc.nimble @@ -12,7 +12,7 @@ requires "nim >= 1.2.0", "chronos", "httputils", "chronicles", - "https://github.com/status-im/news#status", + "https://github.com/status-im/nim-ws", "chronicles", "json_serialization" diff --git a/json_rpc/clients/websocketclient.nim b/json_rpc/clients/websocketclient.nim index 9bdade3..85f45b3 100644 --- a/json_rpc/clients/websocketclient.nim +++ b/json_rpc/clients/websocketclient.nim @@ -1,17 +1,17 @@ import - std/[strtabs, tables], - chronos, + std/[strtabs, tables, uri, strutils], + pkg/[chronos, ws/ws, chronicles], ../client export client -const newsUseChronos = true -include news +logScope: + topics = "JSONRPC-WS-CLIENT" type RpcWebSocketClient* = ref object of RpcClient - transport*: WebSocket - uri*: string + transport*: WSSession + uri*: Uri loop*: Future[void] proc new*(T: type RpcWebSocketClient): T = @@ -30,7 +30,6 @@ method call*(self: RpcWebSocketClient, name: string, 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]() @@ -42,18 +41,20 @@ method call*(self: RpcWebSocketClient, name: string, proc processData(client: RpcWebSocketClient) {.async.} = var error: ref CatchableError + let ws = client.transport try: - while true: - var value = await client.transport.receiveString() - if value == "": + while ws.readystate != ReadyState.Closed: + var value = await ws.recv() + + if value.len == 0: # transmission ends break - client.processMessage(value) + client.processMessage(cast[string](value)) except CatchableError as e: error = e - client.transport.close() + await client.transport.close() client.transport = nil if client.awaiting.len != 0: @@ -65,21 +66,32 @@ proc processData(client: RpcWebSocketClient) {.async.} = 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 +proc connect*(client: RpcWebSocketClient, uri: string, + flags: set[TLSFlags] = { + NoVerifyHost, NoVerifyServerName}) {.async.} = + try: + + let uri = parseUri(uri) + let secure = uri.scheme == "wss" + let port = parseInt(uri.port) + + let ws = await WebSocket.connect( + host = uri.hostname, + port = Port(port), + path = uri.path, + secure=secure, + flags=flags + ) + client.transport = ws + client.uri = uri + + except WebSocketError as exc: + error "WebSocket error", exception = exc.msg + client.loop = processData(client) method close*(client: RpcWebSocketClient) {.async.} = await client.loop.cancelAndWait() if not client.transport.isNil: - client.transport.close() + await client.transport.close() client.transport = nil -