wrapReply now returns string, includes terminator

This commit is contained in:
coffeepots 2018-06-05 13:29:33 +01:00
parent 53baf0bffc
commit 383654f1f5
2 changed files with 6 additions and 7 deletions

View File

@ -1,15 +1,14 @@
import json, asyncdispatch, asyncnet, jsonutils, private / debugutils
proc wrapReply*(id: JsonNode, value: JsonNode, error: JsonNode): JsonNode =
return %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id}
proc wrapReply*(id: JsonNode, value: JsonNode, error: JsonNode): string =
let node = %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id}
return $node & "\c\l"
proc sendError*(client: AsyncSocket, code: int, msg: string, id: JsonNode, data: JsonNode = newJNull()) {.async.} =
## Send error message to client
let error = %{"code": %(code), "message": %msg, "data": data}
ifDebug: echo "Send error json: ", wrapReply(newJNull(), error, id).pretty, "\c\l"
# REVIEW: prefer in-place appending instead of string concatenation
# (see the similar comment in clientdispatch.nim)
result = client.send($wrapReply(id, newJNull(), error) & "\c\l")
ifDebug: echo "Send error json: ", wrapReply(newJNull(), error, id)
result = client.send(wrapReply(id, newJNull(), error))
proc sendJsonError*(state: RpcJsonError, client: AsyncSocket, id: JsonNode, data = newJNull()) {.async.} =
## Send client response for invalid json state

View File

@ -21,7 +21,7 @@ proc processMessage(server: RpcServer, client: AsyncSocket, line: string) {.asyn
await client.sendError(METHOD_NOT_FOUND, "Method not found", id, %(methodName & " is not a registered method."))
else:
let callRes = await server.procs[methodName](node["params"])
await client.send($wrapReply(id, callRes, newJNull()) & "\c\l")
await client.send(wrapReply(id, callRes, newJNull()))
proc processClient(server: RpcServer, client: AsyncSocket) {.async.} =
while true: