diff options
author | 2024-03-24 21:47:33 +0100 | |
---|---|---|
committer | 2024-03-24 21:47:33 +0100 | |
commit | b4e12fd1165b5e3960a1294dadec45eb40893b37 (patch) | |
tree | 214908f3aaf1bea984adfdce4d9b755ba71be670 /src/main.rs | |
parent | 7e1eb27a06e5545b3d1b77b5998dc0463df27d70 (diff) |
Unstable commit, won't compile.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index d782d2b..07a3bf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,20 @@ +use crate::item_update::ItemUpdate; +use crate::subscription::{Subscription, SubscriptionMode}; +use crate::subscription_listener::SubscriptionListener; + use futures::stream::StreamExt; use futures::SinkExt; +use lightstreamer_client::lightstreamer_client::LightstreamerClient; use reqwest::Client; use serde_urlencoded; use std::error::Error; use std::sync::Arc; -use tokio_tungstenite::{connect_async, tungstenite::protocol::Message}; use tokio::sync::Mutex; +use tokio_tungstenite::{connect_async, tungstenite::protocol::Message}; + +mod item_update; +mod subscription; +mod subscription_listener; async fn establish_persistent_http_connection( session_id_shared: Arc<Mutex<String>>, @@ -116,7 +125,12 @@ async fn subscribe_to_channel(session_id: String) -> Result<(), reqwest::Error> // Function to subscribe to a channel using WebSocket async fn subscribe_to_channel_ws( session_id: String, - mut write: futures::stream::SplitSink<tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>, tokio_tungstenite::tungstenite::protocol::Message>, + mut write: futures::stream::SplitSink< + tokio_tungstenite::WebSocketStream< + tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>, + >, + tokio_tungstenite::tungstenite::protocol::Message, + >, ) -> Result<(), Box<dyn Error>> { // Example subscription to ITEM1 in MERGE mode from the DEMO adapter set let sub_params = [ @@ -131,15 +145,14 @@ async fn subscribe_to_channel_ws( let encoded_sub_params = serde_urlencoded::to_string(&sub_params)?; // Send the subscription message - write - .send(Message::Text(encoded_sub_params)) - .await?; + write.send(Message::Text(encoded_sub_params)).await?; println!("Subscribed to channel with session ID: {}", session_id); Ok(()) } +/* #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { @@ -172,4 +185,34 @@ async fn main() -> Result<(), Box<dyn Error>> { task2.await?; Ok(()) -}
\ No newline at end of file +} +*/ + +pub struct MySubscriptionListener {} + +impl SubscriptionListener for MySubscriptionListener { + fn on_item_update(&mut self, update: ItemUpdate) { + println!( + "UPDATE {} {}", + update.get_value("stock_name").unwrap(), + update.get_value("last_price").unwrap() + ); + } +} + +#[tokio::main] +async fn main() -> Result<(), Box<dyn Error>> { + let mut subscription = Subscription::new( + SubscriptionMode::Merge, + Some(vec!["item1".to_string(),"item2".to_string(),"item3".to_string()]), + Some(vec!["stock_name".to_string(),"last_price".to_string()]), + )?; + + subscription.add_listener(Box::new(MySubscriptionListener {})); + + let client = LightstreamerClient::new("http://push.lightstreamer.com/lightstreamer", "DEMO")?; + + println!("Subscription: {:?}", subscription); + + Ok(()) +} |