mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-17 07:04:36 +00:00
more refactor
This commit is contained in:
1 parent
7aa33d19b0
commit
22cf399f5c
11 files changed
+27
-135
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
pub mod obs_gateway_websocket_handler;
|
||||
pub mod obs_twitch_thread;
|
||||
pub mod pubsub_gateway;
|
||||
pub mod obs_twitch_thread;
|
||||
@@ -25,6 +25,9 @@ pub async fn obs_gateway_websocket_handler(
|
||||
) -> Result<HttpResponse, Error> {
|
||||
info!(">>>>>> obs_ws_index");
|
||||
|
||||
let user_id = server_state.twitch_oauth_temp.temp_oauth_user_id.parse::<u32>().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::<u32>().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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,15 +8,26 @@ use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
// TODO: Let's make this an Arc<RwLock<Map<Token, Thread>>>
|
||||
// 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<RwLock<PollingTwitchWebsocketClient>>,
|
||||
last_requested: Arc<RwLock<Instant>>,
|
||||
}
|
||||
|
||||
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)),
|
||||
}
|
||||
|
||||
@@ -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<HttpResponse, Error> {
|
||||
error!(">>>>>> ws_index");
|
||||
ws::start(WebSocket::new(), &r, stream)
|
||||
}
|
||||
|
||||
struct WebSocket {}
|
||||
|
||||
impl Actor for WebSocket {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
|
||||
fn started(&mut self, _ctx: &mut Self::Context) {
|
||||
error!(">>>>>> actor started");
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocket {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
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<Self>;
|
||||
//}
|
||||
//
|
||||
///// Handler for ws::Message message
|
||||
//impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
|
||||
// fn handle(
|
||||
// &mut self,
|
||||
// msg: Result<ws::Message, ws::ProtocolError>,
|
||||
// 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<HttpResponse, Error> {
|
||||
// let resp = ws::start(MyWs {}, &req, stream);
|
||||
// println!("{:?}", resp);
|
||||
// resp
|
||||
//}
|
||||
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod oauth_token_builder;
|
||||
Reference in new issue
Block a user