Struct jsonrpsee_http_server::RpcModule [−][src]
pub struct RpcModule<Context> { /* fields omitted */ }Expand description
Sets of JSON-RPC methods can be organized into a “module“s that are in turn registered on the server or,
alternatively, merged with other modules to construct a cohesive API. RpcModule wraps an additional context
argument that can be used to access data during call execution.
Implementations
Register a new synchronous RPC method, which computes the response with the given callback.
pub fn register_async_method<R, Fun, Fut>(
&mut self,
method_name: &'static str,
callback: Fun
) -> Result<MethodResourcesBuilder<'_>, Error> where
R: 'static + Serialize + Send + Sync,
Fut: Future<Output = Result<R, Error>> + Send,
Fun: 'static + Fn(Params<'static>, Arc<Context>) -> Fut + Copy + Send + Sync,
pub fn register_async_method<R, Fun, Fut>(
&mut self,
method_name: &'static str,
callback: Fun
) -> Result<MethodResourcesBuilder<'_>, Error> where
R: 'static + Serialize + Send + Sync,
Fut: Future<Output = Result<R, Error>> + Send,
Fun: 'static + Fn(Params<'static>, Arc<Context>) -> Fut + Copy + Send + Sync,
Register a new asynchronous RPC method, which computes the response with the given callback.
Register a new blocking synchronous RPC method, which computes the response with the given callback.
Unlike the regular register_method, this method can block its thread and perform expensive computations.
pub fn register_subscription<F>(
&mut self,
subscribe_method_name: &'static str,
notif_method_name: &'static str,
unsubscribe_method_name: &'static str,
callback: F
) -> Result<(), Error> where
Context: 'static + Send + Sync,
F: 'static + Fn(Params<'_>, SubscriptionSink, Arc<Context>) -> Result<(), Error> + Send + Sync,
pub fn register_subscription<F>(
&mut self,
subscribe_method_name: &'static str,
notif_method_name: &'static str,
unsubscribe_method_name: &'static str,
callback: F
) -> Result<(), Error> where
Context: 'static + Send + Sync,
F: 'static + Fn(Params<'_>, SubscriptionSink, Arc<Context>) -> Result<(), Error> + Send + Sync,
Register a new RPC subscription that invokes s callback on every subscription call.
This method ensures that the subscription_method_name and unsubscription_method_name are unique.
The notif_method_name argument sets the content of the method field in the JSON document that
the server sends back to the client. The uniqueness of this value is not machine checked and it’s up to
the user to ensure it is not used in any other RpcModule used in the server.
Arguments
subscription_method_name- name of the method to call to initiate a subscriptionnotif_method_name- name of method to be used in the subscription payload (technically a JSON-RPC notification)unsubscription_method- name of the method to call to terminate a subscriptioncallback- A callback to invoke on each subscription; it takes three parameters:Params: JSON-RPC parameters in the subscription call.SubscriptionSink: A sink to send messages to the subscriber.- Context: Any type that can be embedded into the
RpcModule.
Examples
use jsonrpsee_utils::server::rpc_module::RpcModule;
let mut ctx = RpcModule::new(99_usize);
ctx.register_subscription("sub", "notif_name", "unsub", |params, mut sink, ctx| {
let x: usize = params.one()?;
std::thread::spawn(move || {
let sum = x + (*ctx);
sink.send(&sum)
});
Ok(())
});Methods from Deref<Target = Methods>
Merge two Methods’s by adding all MethodCallbacks from other into self.
Fails if any of the methods in other is present already.
Returns the method callback.
Attempt to execute a callback, sending the resulting JSON (success or error) to the specified sink.
Attempt to execute a callback while checking that the call does not exhaust the available resources, sending the resulting JSON (success or error) to the specified sink.
Helper to call a method on the RPC module without having to spin up a server.
The params must be serializable as JSON array, see ToRpcParams for further documentation.
Helper alternative to execute, useful for writing unit tests without having to spin
a server up.
pub async fn test_subscription(
&'_ self,
method: &'_ str,
params: impl ToRpcParams
) -> TestSubscription
pub async fn test_subscription(
&'_ self,
method: &'_ str,
params: impl ToRpcParams
) -> TestSubscription
Test helper that sets up a subscription using the given method. Returns a tuple of the
SubscriptionId and a channel on which subscription JSON payloads can be received.
Returns an Iterator with all the method names registered on this server.
Trait Implementations
Auto Trait Implementations
impl<Context> !RefUnwindSafe for RpcModule<Context>
impl<Context> !UnwindSafe for RpcModule<Context>
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more