|
| 1 | +// Copyright 2019-2021 Parity Technologies (UK) Ltd. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any |
| 4 | +// person obtaining a copy of this software and associated |
| 5 | +// documentation files (the "Software"), to deal in the |
| 6 | +// Software without restriction, including without |
| 7 | +// limitation the rights to use, copy, modify, merge, |
| 8 | +// publish, distribute, sublicense, and/or sell copies of |
| 9 | +// the Software, and to permit persons to whom the Software |
| 10 | +// is furnished to do so, subject to the following |
| 11 | +// conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice |
| 14 | +// shall be included in all copies or substantial portions |
| 15 | +// of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 18 | +// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 19 | +// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 20 | +// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 21 | +// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 22 | +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 24 | +// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | +// DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +use std::net::SocketAddr; |
| 28 | + |
| 29 | +use jsonrpsee::client_transport::ws::{Url, WsTransportClientBuilder}; |
| 30 | +use jsonrpsee::core::client::{Client, ClientBuilder, ClientT, IdKind}; |
| 31 | +use jsonrpsee::rpc_params; |
| 32 | +use jsonrpsee::server::{RpcModule, Server}; |
| 33 | +use jsonrpsee::types::{Id, IdGeneratorFn}; |
| 34 | + |
| 35 | +#[tokio::main] |
| 36 | +async fn main() -> anyhow::Result<()> { |
| 37 | + tracing_subscriber::FmtSubscriber::builder() |
| 38 | + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 39 | + .try_init() |
| 40 | + .expect("setting default subscriber failed"); |
| 41 | + |
| 42 | + let addr = run_server().await?; |
| 43 | + let uri = Url::parse(&format!("ws://{}", addr))?; |
| 44 | + |
| 45 | + let custom_generator = IdGeneratorFn::new(generate_timestamp_id); |
| 46 | + |
| 47 | + let (tx, rx) = WsTransportClientBuilder::default().build(uri).await?; |
| 48 | + let client: Client = ClientBuilder::default().id_format(IdKind::Custom(custom_generator)).build_with_tokio(tx, rx); |
| 49 | + |
| 50 | + let response: String = client.request("say_hello", rpc_params![]).await?; |
| 51 | + tracing::info!("response: {:?}", response); |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +async fn run_server() -> anyhow::Result<SocketAddr> { |
| 57 | + let server = Server::builder().build("127.0.0.1:0").await?; |
| 58 | + let mut module = RpcModule::new(()); |
| 59 | + module.register_method("say_hello", |_, _, _| "lo")?; |
| 60 | + let addr = server.local_addr()?; |
| 61 | + |
| 62 | + let handle = server.start(module); |
| 63 | + |
| 64 | + // In this example we don't care about doing shutdown so let's it run forever. |
| 65 | + // You may use the `ServerHandle` to shut it down or manage it yourself. |
| 66 | + tokio::spawn(handle.stopped()); |
| 67 | + |
| 68 | + Ok(addr) |
| 69 | +} |
| 70 | + |
| 71 | +fn generate_timestamp_id() -> Id<'static> { |
| 72 | + let timestamp = |
| 73 | + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("Time went backwards").as_secs(); |
| 74 | + Id::Number(timestamp) |
| 75 | +} |
0 commit comments