pub struct Server<C> { /* private fields */ }Expand description
A JSON-RPC 2.0 server.
Generic over a context type C that is cloned once per handler invocation.
§Example
use json_rpc::{Server, ServerConfig, Error};
let mut server = Server::<()>::new(ServerConfig::default());
server.register("add", |_: (), (a, b): (i64, i64)| async move {
Ok::<_, Error>(a + b)
});Implementations§
Source§impl<C: Send + Sync + 'static> Server<C>
impl<C: Send + Sync + 'static> Server<C>
Sourcepub fn new(config: ServerConfig) -> Self
pub fn new(config: ServerConfig) -> Self
Creates a new server with the given configuration and no registered methods.
Use ServerConfig::default to accept batches (the default behavior).
Sourcepub fn register<P, R, E, F>(
&mut self,
method: impl Into<String>,
handler: impl Fn(C, P) -> F + Send + Sync + 'static,
)
pub fn register<P, R, E, F>( &mut self, method: impl Into<String>, handler: impl Fn(C, P) -> F + Send + Sync + 'static, )
Registers an async handler for the given method name.
The handler receives an owned clone of the context and deserialized method parameters, and returns a future.
Sourcepub async fn handle(
&self,
ctx: C,
message: RequestMessage,
) -> Option<ResponseMessage>where
C: Clone,
pub async fn handle(
&self,
ctx: C,
message: RequestMessage,
) -> Option<ResponseMessage>where
C: Clone,
Handles a request message and returns the corresponding response message.
The context ctx is consumed and, for batches, cloned once per handler invocation.
Returns None when the message was a notification (or an all-notification batch)
— nothing should be sent back on the transport.
When the server was configured with ServerConfig::accept_batches set to false,
batch requests are rejected with a single -32600 Invalid Request error.