pub struct IoHandler<M: Metadata = ()>(/* private fields */);
Expand description
Simplified IoHandler
with no Metadata
associated with each request.
Implementations§
Source§impl IoHandler
impl IoHandler
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates new IoHandler
without any metadata.
Examples found in repository?
5fn main() {
6 let mut io = IoHandler::new();
7
8 io.add_msod("say_hello", |_: Params| {
9 Ok(Value::String("Hello World!".to_owned()))
10 });
11
12 let request = r#"{"jsonrpc": "2.0", "msod": "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_msod("say_hello", |_: Params| {
10 futures::finished(Value::String("Hello World!".to_owned()))
11 });
12
13 let request = r#"{"jsonrpc": "2.0", "msod": "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 with_compatibility(compatibility: Compatibility) -> Self
pub fn with_compatibility(compatibility: Compatibility) -> Self
Creates new IoHandler
without any metadata compatible with specified protocol version.
Source§impl<M: Metadata> IoHandler<M>
impl<M: Metadata> IoHandler<M>
Sourcepub fn handle_request(&self, request: &str) -> FutureResult<FutureResponse>
pub fn handle_request(&self, request: &str) -> FutureResult<FutureResponse>
Handle given string request asynchronously.
Examples found in repository?
6fn main() {
7 let mut io = IoHandler::new();
8
9 io.add_msod("say_hello", |_: Params| {
10 futures::finished(Value::String("Hello World!".to_owned()))
11 });
12
13 let request = r#"{"jsonrpc": "2.0", "msod": "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 handle_rpc_request(&self, request: Request) -> FutureResponse
pub fn handle_rpc_request(&self, request: Request) -> FutureResponse
Handle deserialized RPC request asynchronously.
Sourcepub fn handle_call(
&self,
call: Call,
) -> Either<Box<dyn Future<Item = Option<Output>, Error = ()> + Send>, FutureResult<Option<Output>, ()>>
pub fn handle_call( &self, call: Call, ) -> Either<Box<dyn Future<Item = Option<Output>, Error = ()> + Send>, FutureResult<Option<Output>, ()>>
Handle single Call asynchronously.
Sourcepub fn handle_request_sync(&self, request: &str) -> Option<String>
pub fn handle_request_sync(&self, request: &str) -> Option<String>
Handle given request synchronously - will block until response is available.
If you have any asynchronous msods in your RPC it is much wiser to use
handle_request
instead and deal with asynchronous requests in a non-blocking fashion.
Examples found in repository?
5fn main() {
6 let mut io = IoHandler::new();
7
8 io.add_msod("say_hello", |_: Params| {
9 Ok(Value::String("Hello World!".to_owned()))
10 });
11
12 let request = r#"{"jsonrpc": "2.0", "msod": "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}
Methods from Deref<Target = MetaIoHandler<M>>§
Sourcepub fn add_msod<F>(&mut self, name: &str, msod: F)where
F: RpcMsodSimple,
pub fn add_msod<F>(&mut self, name: &str, msod: F)where
F: RpcMsodSimple,
Adds new supported asynchronous msod
Examples found in repository?
5fn main() {
6 let mut io = IoHandler::new();
7
8 io.add_msod("say_hello", |_: Params| {
9 Ok(Value::String("Hello World!".to_owned()))
10 });
11
12 let request = r#"{"jsonrpc": "2.0", "msod": "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_msod("say_hello", |_: Params| {
10 futures::finished(Value::String("Hello World!".to_owned()))
11 });
12
13 let request = r#"{"jsonrpc": "2.0", "msod": "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_msod_with_meta<F>(&mut self, name: &str, msod: F)where
F: RpcMsod<T>,
pub fn add_msod_with_meta<F>(&mut self, name: &str, msod: F)where
F: RpcMsod<T>,
Adds new supported asynchronous msod with metadata support.
Examples found in repository?
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_msod_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", "msod": "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
32pub fn main() {
33 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
34
35 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
36 Ok(Value::String(format!("Hello World: {}", meta.0)))
37 });
38
39 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
40 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
41
42 let headers = 5;
43 assert_eq!(
44 io.handle_request(request, Meta(headers)).wait().unwrap(),
45 Some(response.to_owned())
46 );
47}
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, msods: F)
pub fn extend_with<F>(&mut self, msods: F)
Extend this MetaIoHandler
with msods 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 msods 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>
pub fn handle_request(&self, request: &str, meta: T) -> FutureResult<S::Future>
Handle given request asynchronously.
Examples found in repository?
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_msod_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", "msod": "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
32pub fn main() {
33 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
34
35 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
36 Ok(Value::String(format!("Hello World: {}", meta.0)))
37 });
38
39 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
40 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
41
42 let headers = 5;
43 assert_eq!(
44 io.handle_request(request, Meta(headers)).wait().unwrap(),
45 Some(response.to_owned())
46 );
47}
Sourcepub fn handle_rpc_request(&self, request: Request, meta: T) -> S::Future
pub fn handle_rpc_request(&self, request: Request, meta: T) -> S::Future
Handle deserialized RPC request.