import { Client } from "https://deno.land/x/messagepack_rpc@v2.0.3/mod.ts";
Client is a wrapper of a session to send requests and notifications.
import { assert, is } from "https://deno.land/x/unknownutil@v3.2.0/mod.ts";
import { channel } from "https://deno.land/x/streamtools@v0.5.0/mod.ts";
import { Session, Client } from "./mod.ts";
const input = channel<Uint8Array>();
const output = channel<Uint8Array>();
const session = new Session(input.reader, output.writer);
session.dispatcher = {
sum(x, y) {
assert(x, is.Number);
assert(y, is.Number);
return x + y;
},
};
session.start();
// Create a client
const client = new Client(session);
// Send a request and wait for the response.
console.log(await client.call("sum", 1, 2)); // 3
// Send a notification and do not wait for the response.
console.log(client.notify("sum", 1, 2)); // undefined
Constructors
new
Client(session: Session, options?: ClientOptions)Constructs a new client.
Note that the indexer must be unique for each session to avoid message ID conflicts. If multiple clients are created for a single session, specify a single indexer.
Methods
call(method: string, ...params: unknown[]): Promise<unknown>
Calls the method on the server and returns the result.
It sends the request message to the server and waits for the response.
notify(method: string, ...params: unknown[]): void
Notifies the method on the server.
It sends the notification message to the server and does not wait for the result.