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.

Set a list of allowed origins. During the handshake, the Origin header will be checked against the list, connections without a matching origin will be denied. Values should be hostnames with protocol.

builder.set_allowed_origins(["https://example.com"]);

By default allows any Origin.

Will return an error if list is empty. Use allow_all_origins to restore the default.

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);

Restores the default behavior of allowing connections with Origin header containing any value. This will undo any list set by set_allowed_origins.

Set a list of allowed hosts. During the handshake, the Host header will be checked against the list. Connections without a matching host will be denied. Values should be hostnames without protocol.

builder.set_allowed_hosts(["example.com"]);

By default allows any Host.

Will return an error if list is empty. Use allow_all_hosts to restore the default.

Restores the default behavior of allowing connections with Host header containing any value. This will undo any list set by set_allowed_hosts.

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

Default: tokio::spawn

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)));

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