aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/pubsub_server.rs
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/pubsub_server.rs
parentcc1d61c401e52ba3b6cd264c5400fb7ab52522dc (diff)
jsonrpc: spawn task when handle new request
Diffstat (limited to 'jsonrpc/examples/pubsub_server.rs')
-rw-r--r--jsonrpc/examples/pubsub_server.rs23
1 files changed, 18 insertions, 5 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;
});
}