This version of the module is deprecated: Moved to moonbitstack/moonjrpc.
JSON-RPC 2.0 for MoonBit: messages, a method table, two-way calls and framing.
moon add moonbitstack/moonjsonrpclet methods = @moonjsonrpc.Methods::new()
methods.typed("sum", (numbers : Array[Int]) => numbers.fold(init=0, (a, b) => a + b))
methods.call("no", _ => @moonjsonrpc.fail(7, "said no"))
methods.notice("log", params => println(params))
let answer = methods.body(request_body) // None when there is nothing to send| Input | Answer |
|---|---|
| Not JSON | -32700 |
| Not a message | -32600, with its identifier if one can be read |
| A method nobody registered | -32601 |
| Parameters typed cannot decode | -32602, with the reason in data |
| fail(code, message, data=...) | That error object |
| Any other error from a handler | -32603 with no detail; Methods::new(on_error=...) hears it |
| An array | A batch; batch=false refuses it with -32600 |
let peer = @peer.Peer::new(@peer.lines(@stdio.stdin, @stdio.stdout), methods~)
@async.with_task_group(async fn(group) {
group.spawn_bg(no_wait=true, () => peer.run())
let root = peer.call("roots/list")
peer.notify("log", params=root)
})| Error from Peer::call | Meaning |
|---|---|
| Failed(failure) | The other side answered with an error object |
| TimedOut(name~, limit~) | No answer in time. The default is 60 seconds; timeout_ms=None waits as long as it takes |
| Closed(name~) | The other side went away |
pub(open) trait Frames {
async fn read(Self) -> String?
async fn write(Self, String) -> Unit
}Install
Download zipJSON-RPC 2.0 for MoonBit: messages, a method table, two-way calls and framing.