mirror of
https://github.com/status-im/nim-json-rpc.git
synced 2025-02-23 09:48:21 +00:00
28 lines
655 B
Nim
28 lines
655 B
Nim
import asyncdispatch, asyncnet, json, tables
|
|
|
|
type
|
|
RpcProc* = proc (params: JsonNode): JsonNode
|
|
|
|
RpcServer* = ref object
|
|
socket*: AsyncSocket
|
|
port*: Port
|
|
address*: string
|
|
procs*: TableRef[string, RpcProc]
|
|
|
|
RpcProcError* = ref object of Exception
|
|
code*: int
|
|
data*: JsonNode
|
|
|
|
proc newRpcServer*(address: string, port: Port = Port(8545)): RpcServer =
|
|
RpcServer(
|
|
socket: newAsyncSocket(),
|
|
port: port,
|
|
address: address,
|
|
procs: newTable[string, RpcProc]()
|
|
)
|
|
|
|
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
|
|
server.procs[name] = rpc
|
|
|
|
proc unRegisterAll*(server: RpcServer) = server.procs.clear
|