Struct jsonrpsee_ws_server::WsServerBuilder
source · [−]pub struct WsServerBuilder<M = ()> { /* private fields */ }
Expand description
Builder to configure and create a JSON-RPC Websocket server
Implementations
sourceimpl<M> Builder<M>
impl<M> Builder<M>
sourcepub fn max_request_body_size(self, size: u32) -> Self
pub fn max_request_body_size(self, size: u32) -> Self
Set the maximum size of a request body in bytes. Default is 10 MiB.
sourcepub fn max_response_body_size(self, size: u32) -> Self
pub fn max_response_body_size(self, size: u32) -> Self
Set the maximum size of a response body in bytes. Default is 10 MiB.
sourcepub fn max_connections(self, max: u64) -> Self
pub fn max_connections(self, max: u64) -> Self
Set the maximum number of connections allowed. Default is 100.
sourcepub fn batch_requests_supported(self, supported: bool) -> Self
pub fn batch_requests_supported(self, supported: bool) -> Self
Enables or disables support of batch requests. By default, support is enabled.
sourcepub fn max_subscriptions_per_connection(self, max: u32) -> Self
pub fn max_subscriptions_per_connection(self, max: u32) -> Self
Set the maximum number of connections allowed. Default is 1024.
sourcepub fn register_resource(
self,
label: &'static str,
capacity: u16,
default: u16
) -> Result<Self, Error>
pub fn register_resource(
self,
label: &'static str,
capacity: u16,
default: u16
) -> Result<Self, Error>
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.
sourcepub fn set_allowed_origins<Origin, List>(
self,
list: List
) -> Result<Self, Error> where
List: IntoIterator<Item = Origin>,
Origin: Into<String>,
pub fn set_allowed_origins<Origin, List>(
self,
list: List
) -> Result<Self, Error> where
List: IntoIterator<Item = Origin>,
Origin: Into<String>,
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.
sourcepub fn set_middleware<T: Middleware>(self, middleware: T) -> Builder<T>
pub fn set_middleware<T: Middleware>(self, middleware: T) -> Builder<T>
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);
sourcepub fn allow_all_origins(self) -> Self
pub fn allow_all_origins(self) -> Self
Restores the default behavior of allowing connections with Origin
header
containing any value. This will undo any list set by set_allowed_origins
.
sourcepub fn set_allowed_hosts<Host, List>(self, list: List) -> Result<Self, Error> where
List: IntoIterator<Item = Host>,
Host: Into<String>,
pub fn set_allowed_hosts<Host, List>(self, list: List) -> Result<Self, Error> where
List: IntoIterator<Item = Host>,
Host: Into<String>,
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.
sourcepub fn allow_all_hosts(self) -> Self
pub fn allow_all_hosts(self) -> Self
Restores the default behavior of allowing connections with Host
header
containing any value. This will undo any list set by set_allowed_hosts
.
sourcepub fn custom_tokio_runtime(self, rt: Handle) -> Self
pub fn custom_tokio_runtime(self, rt: Handle) -> Self
Configure a custom tokio::runtime::Handle
to run the server on.
Default: tokio::spawn
sourcepub fn set_id_provider<I: IdProvider + 'static>(self, id_provider: I) -> Self
pub fn set_id_provider<I: IdProvider + 'static>(self, id_provider: I) -> Self
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)));
sourcepub async fn build(self, addrs: impl ToSocketAddrs) -> Result<Server<M>, Error>
pub async fn build(self, addrs: impl ToSocketAddrs) -> Result<Server<M>, Error>
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
Auto Trait Implementations
impl<M = ()> !RefUnwindSafe for Builder<M>
impl<M> Send for Builder<M> where
M: Send,
impl<M> Sync for Builder<M> where
M: Sync,
impl<M> Unpin for Builder<M> where
M: Unpin,
impl<M = ()> !UnwindSafe for Builder<M>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more