aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-23 00:19:58 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-23 00:19:58 +0200
commit2d1a8aea0b9330cd2eaad26eb187644adad6bed9 (patch)
tree6083adaa09ae2f0ef2912f7934cdf0bfafff1654 /jsonrpc/examples
parentcc1d61c401e52ba3b6cd264c5400fb7ab52522dc (diff)
jsonrpc: spawn task when handle new request
Diffstat (limited to 'jsonrpc/examples')
-rw-r--r--jsonrpc/examples/pubsub_server.rs23
-rw-r--r--jsonrpc/examples/server.rs7
-rw-r--r--jsonrpc/examples/tokio_server/src/main.rs6
3 files changed, 27 insertions, 9 deletions
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<Value, Error> {
- let sub = chan.new_subscription().await;
+ async fn log_subscribe(
+ &self,
+ chan: ArcChannel,
+ method: String,
+ _params: Value,
+ ) -> Result<Value, Error> {
+ 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<Value, Error> {
+ async fn log_unsubscribe(
+ &self,
+ chan: ArcChannel,
+ _method: String,
+ params: Value,
+ ) -> Result<Value, Error> {
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;
}