pub struct MetaIoHandler<T: Metadata, S: Middleware<T> = Noop> { /* private fields */ }
Expand description
Request handler
By default compatible only with jsonrpc v2
Implementations§
Source§impl<T: Metadata> MetaIoHandler<T>
impl<T: Metadata> MetaIoHandler<T>
Sourcepub fn with_compatibility(compatibility: Compatibility) -> Self
pub fn with_compatibility(compatibility: Compatibility) -> Self
Creates new MetaIoHandler
compatible with specified protocol version.
Source§impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S>
impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S>
Sourcepub fn new(compatibility: Compatibility, middleware: S) -> Self
pub fn new(compatibility: Compatibility, middleware: S) -> Self
Creates new MetaIoHandler
Sourcepub fn with_middleware(middleware: S) -> Self
pub fn with_middleware(middleware: S) -> Self
Creates new MetaIoHandler
with specified middleware.
Examples found in repository?
34pub fn main() {
35 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
36
37 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| {
38 Ok(Value::String(format!("Hello World: {}", meta.0)))
39 });
40
41 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
42 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
43
44 let headers = 5;
45 assert_eq!(
46 io.handle_request(request, Meta(headers)).wait().unwrap(),
47 Some(response.to_owned())
48 );
49}
Sourcepub fn add_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSimple,
pub fn add_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSimple,
Adds new supported asynchronous method
Examples found in repository?
5fn main() {
6 let mut io = IoHandler::new();
7
8 io.add_method("say_hello", |_: Params| {
9 Ok(Value::String("Hello World!".to_owned()))
10 });
11
12 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
13 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
14
15 assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
16}
More examples
6fn main() {
7 let mut io = IoHandler::new();
8
9 io.add_method("say_hello", |_: Params| {
10 futures::finished(Value::String("Hello World!".to_owned()))
11 });
12
13 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
14 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
15
16 assert_eq!(io.handle_request(request).wait().unwrap(), Some(response.to_owned()));
17}
Sourcepub fn add_notification<F>(&mut self, name: &str, notification: F)where
F: RpcNotificationSimple,
pub fn add_notification<F>(&mut self, name: &str, notification: F)where
F: RpcNotificationSimple,
Adds new supported notification
Sourcepub fn add_method_with_meta<F>(&mut self, name: &str, method: F)where
F: RpcMethod<T>,
pub fn add_method_with_meta<F>(&mut self, name: &str, method: F)where
F: RpcMethod<T>,
Adds new supported asynchronous method with metadata support.
Examples found in repository?
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| {
14 Ok(Value::String(format!("Hello World: {}", meta.0)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
19
20 let headers = 5;
21 assert_eq!(
22 io.handle_request(request, Meta(headers)).wait().unwrap(),
23 Some(response.to_owned())
24 );
25}
More examples
34pub fn main() {
35 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
36
37 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| {
38 Ok(Value::String(format!("Hello World: {}", meta.0)))
39 });
40
41 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
42 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
43
44 let headers = 5;
45 assert_eq!(
46 io.handle_request(request, Meta(headers)).wait().unwrap(),
47 Some(response.to_owned())
48 );
49}
Sourcepub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)where
F: RpcNotification<T>,
pub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)where
F: RpcNotification<T>,
Adds new supported notification with metadata support.
Sourcepub fn extend_with<F>(&mut self, methods: F)
pub fn extend_with<F>(&mut self, methods: F)
Extend this MetaIoHandler
with methods defined elsewhere.
Sourcepub fn handle_request_sync(&self, request: &str, meta: T) -> Option<String>
pub fn handle_request_sync(&self, request: &str, meta: T) -> Option<String>
Handle given request synchronously - will block until response is available.
If you have any asynchronous methods in your RPC it is much wiser to use
handle_request
instead and deal with asynchronous requests in a non-blocking fashion.
Sourcepub fn handle_request(
&self,
request: &str,
meta: T,
) -> FutureResult<S::Future, S::CallFuture>
pub fn handle_request( &self, request: &str, meta: T, ) -> FutureResult<S::Future, S::CallFuture>
Handle given request asynchronously.
Examples found in repository?
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| {
14 Ok(Value::String(format!("Hello World: {}", meta.0)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
19
20 let headers = 5;
21 assert_eq!(
22 io.handle_request(request, Meta(headers)).wait().unwrap(),
23 Some(response.to_owned())
24 );
25}
More examples
34pub fn main() {
35 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
36
37 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| {
38 Ok(Value::String(format!("Hello World: {}", meta.0)))
39 });
40
41 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
42 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
43
44 let headers = 5;
45 assert_eq!(
46 io.handle_request(request, Meta(headers)).wait().unwrap(),
47 Some(response.to_owned())
48 );
49}
Sourcepub fn handle_rpc_request(
&self,
request: Request,
meta: T,
) -> FutureRpcResult<S::Future, S::CallFuture>
pub fn handle_rpc_request( &self, request: Request, meta: T, ) -> FutureRpcResult<S::Future, S::CallFuture>
Handle deserialized RPC request.
Sourcepub fn handle_call(
&self,
call: Call,
meta: T,
) -> Either<S::CallFuture, Either<FutureOutput, FutureResult<Option<Output>, ()>>>
pub fn handle_call( &self, call: Call, meta: T, ) -> Either<S::CallFuture, Either<FutureOutput, FutureResult<Option<Output>, ()>>>
Handle single call asynchronously.