From c2eb31cbb2b75c6686fc2efcddf7b194a7f3a5ea Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 26 Sep 2021 20:33:51 -0400 Subject: [PATCH] listen wip --- src/main.rs | 26 +++++++++++++++++++++++--- src/twitch/websocket_client.rs | 19 +++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 82e7f304fb..1c80aa7e57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,12 +113,32 @@ async fn run() -> AnyhowResult<()> { println!("Starting polling thread..."); client.start_ping_thread().await; + + // We want to subscribe to moderator actions on channel with id 1234 + // as if we were a user with id 4321 that is moderator on the channel. + let chat_mod_actions = pubsub::moderation::ChatModeratorActions { + user_id: 4321, + channel_id: 1234, + }.into_topic(); + + // Listen to follows as well + let follows = pubsub::following::Following { channel_id: 1234 }.into_topic(); + + println!("Begin LISTEN..."); + client.listen("auth_token", &[chat_mod_actions, follows]).await; + + println!("Try read next..."); - client.try_next().await?; + let r = client.try_next().await?; + println!("Result: {:?}", r); + println!("Try read next..."); - client.try_next().await?; + let r = client.try_next().await?; + println!("Result: {:?}", r); + println!("Try read next..."); - client.try_next().await?; + let r = client.try_next().await?; + println!("Result: {:?}", r); println!("Sleep..."); std::thread::sleep(Duration::from_millis(30000)); diff --git a/src/twitch/websocket_client.rs b/src/twitch/websocket_client.rs index 4c060a21b9..901cc8b23c 100644 --- a/src/twitch/websocket_client.rs +++ b/src/twitch/websocket_client.rs @@ -52,6 +52,15 @@ impl PollingTwitchWebsocketClient { } } + pub async fn listen<'t>(&mut self, auth_token: &str, topics: &'t [Topics]) -> AnyhowResult<()> { + match self.client.lock() { + Err(e) => Err(anyhow!("unlock client err: {:?}", e)), + Ok(mut client) => { + client.listen(auth_token, topics).await + } + } + } + /// Start a pinging background thread. /// Per Twitch PubSub docs, /// To keep the server from closing the connection, clients must send a PING command @@ -181,16 +190,6 @@ impl TwitchWebsocketClient { } pub async fn listen<'t>(&mut self, auth_token: &str, topics: &'t [Topics]) -> AnyhowResult<()> { - //// We want to subscribe to moderator actions on channel with id 1234 - //// as if we were a user with id 4321 that is moderator on the channel. - //let chat_mod_actions = pubsub::moderation::ChatModeratorActions { - // user_id: 4321, - // channel_id: 1234, - //}.into_topic(); - - //// Listen to follows as well - //let follows = pubsub::following::Following { channel_id: 1234 }.into_topic(); - let mut socket = match self.socket { None => return Err(anyhow!("not connected")), Some(ref mut socket) => socket,