From 2d1a8aea0b9330cd2eaad26eb187644adad6bed9 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 23 May 2024 00:19:58 +0200 Subject: jsonrpc: spawn task when handle new request --- jsonrpc/examples/pubsub_server.rs | 23 ++++++++++++++++++----- jsonrpc/examples/server.rs | 7 +++++-- jsonrpc/examples/tokio_server/src/main.rs | 6 ++++-- 3 files changed, 27 insertions(+), 9 deletions(-) (limited to 'jsonrpc/examples') diff --git a/jsonrpc/examples/pubsub_server.rs b/jsonrpc/examples/pubsub_server.rs index 739e6d5..4b77c45 100644 --- a/jsonrpc/examples/pubsub_server.rs +++ b/jsonrpc/examples/pubsub_server.rs @@ -1,8 +1,9 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use karyon_core::async_util::sleep; use karyon_jsonrpc::{rpc_impl, rpc_pubsub_impl, ArcChannel, Error, Server, SubscriptionID}; struct Calc {} @@ -25,8 +26,13 @@ impl Calc { #[rpc_pubsub_impl] impl Calc { - async fn log_subscribe(&self, chan: ArcChannel, _params: Value) -> Result { - let sub = chan.new_subscription().await; + async fn log_subscribe( + &self, + chan: ArcChannel, + method: String, + _params: Value, + ) -> Result { + let sub = chan.new_subscription(&method).await; let sub_id = sub.id.clone(); smol::spawn(async move { loop { @@ -42,7 +48,12 @@ impl Calc { Ok(serde_json::json!(sub_id)) } - async fn log_unsubscribe(&self, chan: ArcChannel, params: Value) -> Result { + async fn log_unsubscribe( + &self, + chan: ArcChannel, + _method: String, + params: Value, + ) -> Result { let sub_id: SubscriptionID = serde_json::from_value(params)?; chan.remove_subscription(&sub_id).await; Ok(serde_json::json!(true)) @@ -64,6 +75,8 @@ fn main() { .expect("Build a new server"); // Start the server - server.start().await.expect("Start the server"); + server.start().await; + + sleep(Duration::MAX).await; }); } diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs index 5b951cd..470bd02 100644 --- a/jsonrpc/examples/server.rs +++ b/jsonrpc/examples/server.rs @@ -1,8 +1,9 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use karyon_core::async_util::sleep; use karyon_jsonrpc::{rpc_impl, Error, Server}; struct Calc { @@ -56,6 +57,8 @@ fn main() { .expect("start a new server"); // Start the server - server.start().await.unwrap(); + server.start().await; + + sleep(Duration::MAX).await; }); } diff --git a/jsonrpc/examples/tokio_server/src/main.rs b/jsonrpc/examples/tokio_server/src/main.rs index ce77cd3..d70a46a 100644 --- a/jsonrpc/examples/tokio_server/src/main.rs +++ b/jsonrpc/examples/tokio_server/src/main.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -56,5 +56,7 @@ async fn main() { .expect("start a new server"); // Start the server - server.start().await.unwrap(); + server.start().await; + + tokio::time::sleep(Duration::MAX).await; } -- cgit v1.2.3