pub struct WsServerBuilder<M = ()> { /* private fields */ }
Expand description

Builder to configure and create a JSON-RPC Websocket server

Implementations

Create a default server builder.

Set the maximum size of a request body in bytes. Default is 10 MiB.

Set the maximum size of a response body in bytes. Default is 10 MiB.

Set the maximum number of connections allowed. Default is 100.

Enables or disables support of batch requests. By default, support is enabled.

Set the maximum number of connections allowed. Default is 1024.

Register a new resource kind. Errors if label is already registered, or if the number of registered resources on this server instance would exceed 8.

See the module documentation for resurce_limiting for details.

Add a middleware to the builder Middleware.

use std::time::Instant;

use jsonrpsee_core::middleware::Middleware;
use jsonrpsee_ws_server::WsServerBuilder;

#[derive(Clone)]
struct MyMiddleware;

impl Middleware for MyMiddleware {
    type Instant = Instant;

    fn on_request(&self) -> Instant {
        Instant::now()
    }

    fn on_result(&self, name: &str, success: bool, started_at: Instant) {
        println!("Call to '{}' took {:?}", name, started_at.elapsed());
    }
}

let builder = WsServerBuilder::new().set_middleware(MyMiddleware);

Configure a custom tokio::runtime::Handle to run the server on.

Default: tokio::spawn

Configure the interval at which pings are submitted.

This option is used to keep the connection alive, and is just submitting Ping frames, without making any assumptions about when a Pong frame should be received.

Default: 60 seconds.

Examples
use std::time::Duration;
use jsonrpsee_ws_server::WsServerBuilder;

// Set the ping interval to 10 seconds.
let builder = WsServerBuilder::default().ping_interval(Duration::from_secs(10));

Configure custom subscription ID provider for the server to use to when getting new subscription calls.

You may choose static dispatch or dynamic dispatch because IdProvider is implemented for Box<T>.

Default: RandomIntegerIdProvider.

Examples
use jsonrpsee_ws_server::{WsServerBuilder, RandomStringIdProvider, IdProvider};

// static dispatch
let builder1 = WsServerBuilder::default().set_id_provider(RandomStringIdProvider::new(16));

// or dynamic dispatch
let builder2 = WsServerBuilder::default().set_id_provider(Box::new(RandomStringIdProvider::new(16)));

Sets access control settings.

Finalize the configuration of the server. Consumes the Builder.

#[tokio::main]
async fn main() {
  let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
  let occupied_addr = listener.local_addr().unwrap();
  let addrs: &[std::net::SocketAddr] = &[
      occupied_addr,
      "127.0.0.1:0".parse().unwrap(),
  ];
  assert!(jsonrpsee_ws_server::WsServerBuilder::default().build(occupied_addr).await.is_err());
  assert!(jsonrpsee_ws_server::WsServerBuilder::default().build(addrs).await.is_ok());
}

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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