diff --git a/crates/service/obs_gateway/src/endpoints/oauth_begin.rs b/crates/service/obs_gateway/src/endpoints/oauth_begin.rs index ac163d0325..9483acf26c 100644 --- a/crates/service/obs_gateway/src/endpoints/oauth_begin.rs +++ b/crates/service/obs_gateway/src/endpoints/oauth_begin.rs @@ -1,6 +1,7 @@ use actix_http::{StatusCode, header}; use actix_web::{HttpRequest, web, HttpResponse, ResponseError, HttpResponseBuilder}; use crate::ObsGatewayServerState; +use crate::twitch::oauth::oauth_token_builder::get_oauth_token_builder; use http_server_common::response::response_error_helpers::to_simple_json_error; use http_server_common::response::response_success_helpers::simple_json_success; use http_server_common::response::to_json_success_response::to_json_success_response; @@ -11,7 +12,6 @@ use std::sync::Arc; use twitch_api2::twitch_oauth2::ClientId; use twitch_oauth2::tokens::UserTokenBuilder; use twitch_oauth2::{Scope, ClientSecret}; -use crate::twitch::oauth_token_builder::get_oauth_token_builder; #[derive(Serialize)] pub struct OauthBeginEnrollResult { diff --git a/crates/service/obs_gateway/src/endpoints/oauth_begin_redirect.rs b/crates/service/obs_gateway/src/endpoints/oauth_begin_redirect.rs index 55cc60187b..65f59d7e9b 100644 --- a/crates/service/obs_gateway/src/endpoints/oauth_begin_redirect.rs +++ b/crates/service/obs_gateway/src/endpoints/oauth_begin_redirect.rs @@ -1,6 +1,7 @@ use actix_http::{StatusCode, header}; use actix_web::{HttpRequest, web, HttpResponse, ResponseError, HttpResponseBuilder}; use crate::ObsGatewayServerState; +use crate::twitch::oauth::oauth_token_builder::get_oauth_token_builder; use http_server_common::response::response_error_helpers::to_simple_json_error; use http_server_common::response::response_success_helpers::simple_json_success; use http_server_common::response::to_json_success_response::to_json_success_response; @@ -11,7 +12,6 @@ use std::sync::Arc; use twitch_api2::twitch_oauth2::ClientId; use twitch_oauth2::tokens::UserTokenBuilder; use twitch_oauth2::{Scope, ClientSecret}; -use crate::twitch::oauth_token_builder::get_oauth_token_builder; #[derive(Serialize)] pub struct OauthBeginEnrollResult { diff --git a/crates/service/obs_gateway/src/endpoints/oauth_end.rs b/crates/service/obs_gateway/src/endpoints/oauth_end.rs index 0e19146801..a75a874182 100644 --- a/crates/service/obs_gateway/src/endpoints/oauth_end.rs +++ b/crates/service/obs_gateway/src/endpoints/oauth_end.rs @@ -1,7 +1,7 @@ use actix_http::StatusCode; use actix_web::{HttpResponse, HttpRequest, web, ResponseError}; use crate::server_state::ObsGatewayServerState; -use crate::twitch::oauth_token_builder::get_oauth_token_builder; +use crate::twitch::oauth::oauth_token_builder::get_oauth_token_builder; use http_server_common::response::response_error_helpers::to_simple_json_error; use log::error; use log::info; diff --git a/crates/service/obs_gateway/src/endpoints_ws/mod.rs b/crates/service/obs_gateway/src/endpoints_ws/mod.rs index cfbaddd858..78235255e4 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/mod.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/mod.rs @@ -1,3 +1,2 @@ pub mod obs_gateway_websocket_handler; -pub mod obs_twitch_thread; -pub mod pubsub_gateway; \ No newline at end of file +pub mod obs_twitch_thread; \ No newline at end of file diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs index 05e97a412d..e011ce535b 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs @@ -25,6 +25,9 @@ pub async fn obs_gateway_websocket_handler( ) -> Result { info!(">>>>>> obs_ws_index"); + let user_id = server_state.twitch_oauth_temp.temp_oauth_user_id.parse::().unwrap(); + let auth_token = server_state.twitch_oauth_temp.temp_oauth_access_token.clone(); + let mut client = PollingTwitchWebsocketClient::new().unwrap(); info!("Connecting to Twitch PubSub..."); @@ -32,57 +35,24 @@ pub async fn obs_gateway_websocket_handler( info!("Connected to Twitch PubSub"); - //println!("Starting polling thread..."); - //client.start_ping_thread().await; - info!("Sending Twitch PubSub PING..."); - client.send_ping().await.unwrap(); info!("Try read next from Twitch PubSub..."); let r = client.try_next().await.unwrap(); info!("Twitch PubSub Result: {:?}", r); - // User: vocodes - //let user_id = 652567283; - - let user_id = server_state.twitch_oauth_temp.temp_oauth_user_id.parse::().unwrap(); - - let bit_topic = pubsub::channel_bits::ChannelBitsEventsV2 { - channel_id: user_id, - }.into_topic(); - - let points_topic = pubsub::channel_points::ChannelPointsChannelV1 { - channel_id: user_id, - }.into_topic(); - - let cheer_topic = pubsub::channel_cheer::ChannelCheerEventsPublicV1 { - channel_id: user_id, - }.into_topic(); - - let sub_topic = pubsub::channel_subscriptions::ChannelSubscribeEventsV1 { - channel_id: user_id, - }.into_topic(); - - //let topics = [bit_topic, points_topic, cheer_topic, sub_topic]; - - let topics = build_pubsub_topics_for_user(user_id); - - let auth_token = server_state.twitch_oauth_temp.temp_oauth_access_token.clone(); - info!("Begin TwitchPubSub LISTEN on authenticated OAuth topics..."); - + let topics = build_pubsub_topics_for_user(user_id); client.listen(&auth_token, &topics).await.unwrap(); - //let (tx, rx) = crossbeam::channel::bounded(1); //let client = Arc::new(&self.twitch_client); //let client2 = client.clone(); - //let res = rx.recv().unwrap(); info!("Begin Javascript WebSocket..."); - let websocket = ObsGatewayWebSocket::new(client); + let websocket = ObsGatewayWebSocket::new(user_id, client); ws::start(websocket, &request, stream) } @@ -90,14 +60,16 @@ pub async fn obs_gateway_websocket_handler( /// Websocket behavior struct ObsGatewayWebSocket { //twitch_client: PollingTwitchWebsocketClient, + twitch_user_id: u32, twitch_thread: ObsTwitchThread, } impl ObsGatewayWebSocket { - fn new(twitch_client: PollingTwitchWebsocketClient) -> Self { - let twitch_thread = ObsTwitchThread::new(twitch_client); + fn new(twitch_user_id: u32, twitch_client: PollingTwitchWebsocketClient) -> Self { + let twitch_thread = ObsTwitchThread::new(twitch_user_id, twitch_client); Self { + twitch_user_id, twitch_thread } } diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs index 6a13dba3ec..de9d76fe91 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs @@ -8,15 +8,26 @@ use std::time::Duration; use std::time::Instant; use tokio::runtime::Handle; +// TODO: Let's make this an Arc>> +// so that multiple browser sessions for the same user can +// share the same single backing instance. +// . +// however, this causes events (TTS) to get flushed to just +// a single browser instance. +// . +// maybe just kill other sessions ? browser+uuid -> LRU. + pub struct ObsTwitchThread { + twitch_user_id: u32, twitch_client: Arc>, last_requested: Arc>, } impl ObsTwitchThread { - pub fn new(twitch_client: PollingTwitchWebsocketClient) -> Self { + pub fn new(twitch_user_id: u32, twitch_client: PollingTwitchWebsocketClient) -> Self { let now = Instant::now(); Self { + twitch_user_id, twitch_client: Arc::new(RwLock::new(twitch_client)), last_requested: Arc::new(RwLock::new(now)), } diff --git a/crates/service/obs_gateway/src/endpoints_ws/pubsub_gateway.rs b/crates/service/obs_gateway/src/endpoints_ws/pubsub_gateway.rs deleted file mode 100644 index 96f49d96ec..0000000000 --- a/crates/service/obs_gateway/src/endpoints_ws/pubsub_gateway.rs +++ /dev/null @@ -1,86 +0,0 @@ -use actix::prelude::*; -use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; -use actix_web_actors::ws; -use log::warn; -use log::error; - -pub async fn ws_index(r: HttpRequest, stream: web::Payload) -> Result { - error!(">>>>>> ws_index"); - ws::start(WebSocket::new(), &r, stream) -} - -struct WebSocket {} - -impl Actor for WebSocket { - type Context = ws::WebsocketContext; - - fn started(&mut self, _ctx: &mut Self::Context) { - error!(">>>>>> actor started"); - } -} - -impl StreamHandler> for WebSocket { - fn handle( - &mut self, - msg: Result, - ctx: &mut Self::Context, - ) { - if let Ok(msg) = msg { - error!(">>>>>> streamhandler::handle (msg = {:?})", msg); - match msg { - ws::Message::Text(text) => ctx.text(text), - ws::Message::Binary(bin) => ctx.binary(bin), - ws::Message::Ping(bytes) => ctx.pong(&bytes), - ws::Message::Close(reason) => { - ctx.close(reason); - ctx.stop(); - } - _ => {} - } - } else { - error!(">>>>>> streamhandler::STOP"); - ctx.stop(); - } - } -} - -impl WebSocket { - fn new() -> Self { - Self {} - } -} - -// From the Actix homepage: - -//use actix::{Actor, StreamHandler}; -//use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer}; -//use actix_web_actors::ws; -// -///// Define HTTP actor -//struct MyWs; -// -//impl Actor for MyWs { -// type Context = ws::WebsocketContext; -//} -// -///// Handler for ws::Message message -//impl StreamHandler> for MyWs { -// fn handle( -// &mut self, -// msg: Result, -// ctx: &mut Self::Context, -// ) { -// match msg { -// Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), -// Ok(ws::Message::Text(text)) => ctx.text(text), -// Ok(ws::Message::Binary(bin)) => ctx.binary(bin), -// _ => (), -// } -// } -//} -// -//pub async fn twitch_pubsub_gateway(req: HttpRequest, stream: web::Payload) -> Result { -// let resp = ws::start(MyWs {}, &req, stream); -// println!("{:?}", resp); -// resp -//} \ No newline at end of file diff --git a/crates/service/obs_gateway/src/main.rs b/crates/service/obs_gateway/src/main.rs index 3d731d3d4f..9b315668d2 100644 --- a/crates/service/obs_gateway/src/main.rs +++ b/crates/service/obs_gateway/src/main.rs @@ -29,7 +29,6 @@ use crate::endpoints::oauth_begin::oauth_begin_enroll; use crate::endpoints::oauth_begin_redirect::oauth_begin_enroll_redirect; use crate::endpoints::oauth_end::oauth_end_enroll_from_redirect; use crate::endpoints_ws::obs_gateway_websocket_handler::obs_gateway_websocket_handler; -use crate::endpoints_ws::pubsub_gateway::ws_index; use crate::server_state::{ObsGatewayServerState, EnvConfig, TwitchOauthSecrets, TwitchOauthTemp}; use crate::twitch::polling_websocket_client::PollingTwitchWebsocketClient; use crate::twitch::twitch_client_wrapper::TwitchClientWrapper; @@ -289,10 +288,6 @@ pub async fn serve(server_state: ObsGatewayServerState) -> AnyhowResult<()> .route(web::get().to(oauth_end_enroll_from_redirect)) .route(web::head().to(|| HttpResponse::Ok())) ) - .service(web::resource("/websocket") - .route(web::get().to(ws_index)) - .route(web::head().to(|| HttpResponse::Ok())) - ) ) .service(web::resource("/obs") .route(web::get().to(obs_gateway_websocket_handler)) diff --git a/crates/service/obs_gateway/src/twitch/mod.rs b/crates/service/obs_gateway/src/twitch/mod.rs index 7743cc2916..dadd275d32 100644 --- a/crates/service/obs_gateway/src/twitch/mod.rs +++ b/crates/service/obs_gateway/src/twitch/mod.rs @@ -1,4 +1,4 @@ -pub mod oauth_token_builder; +pub mod oauth; pub mod polling_websocket_client; pub mod pubsub; pub mod twitch_client_wrapper; diff --git a/crates/service/obs_gateway/src/twitch/oauth/mod.rs b/crates/service/obs_gateway/src/twitch/oauth/mod.rs new file mode 100644 index 0000000000..c406e2678c --- /dev/null +++ b/crates/service/obs_gateway/src/twitch/oauth/mod.rs @@ -0,0 +1 @@ +pub mod oauth_token_builder; \ No newline at end of file diff --git a/crates/service/obs_gateway/src/twitch/oauth_token_builder.rs b/crates/service/obs_gateway/src/twitch/oauth/oauth_token_builder.rs similarity index 100% rename from crates/service/obs_gateway/src/twitch/oauth_token_builder.rs rename to crates/service/obs_gateway/src/twitch/oauth/oauth_token_builder.rs