From 4956e44c0a83048909f19f9e24e6815e4f686e4b Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 28 Apr 2023 23:21:55 +0200 Subject: [PATCH] implement Gateway::new() --- Cargo.toml | 3 ++- src/gateway.rs | 63 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1769377..22d8f0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ chrono = "0.4.24" regex = "1.7.3" custom_error = "1.9.2" native-tls = "0.2.11" -tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]} \ No newline at end of file +tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]} +futures-util = "0.3.28" \ No newline at end of file diff --git a/src/gateway.rs b/src/gateway.rs index f56e0d4..61f51ec 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -1,16 +1,24 @@ use std::sync::Arc; use std::sync::Mutex; use std::thread::JoinHandle; +use std::time::Duration; use crate::api::types::*; use crate::api::WebSocketEvent; use crate::errors::ObserverError; use crate::gateway::events::Events; use crate::URLBundle; +use futures_util::SinkExt; +use futures_util::StreamExt; use reqwest::Url; +use serde::Deserialize; +use serde::Serialize; +use serde_json::from_str; use serde_json::to_string; +use tokio::io; use tokio::net::TcpStream; -use tokio_tungstenite::tungstenite::Error; +use tokio_tungstenite::connect_async; +use tokio_tungstenite::tungstenite::error::UrlError; use tokio_tungstenite::MaybeTlsStream; use tokio_tungstenite::WebSocketStream; @@ -21,31 +29,43 @@ implemented [Types] with the trait [`WebSocketEvent`] */ pub struct Gateway<'a> { pub url: String, + pub token: String, pub events: Events<'a>, - socket: Arc>>>>, - thread_handle: Option>, + socket: WebSocketStream>, +} + +#[derive(Deserialize, Serialize, Debug)] +enum GatewayIntervalEvent { + Heartbeat(Option), + Hello(u64), } impl<'a> Gateway<'a> { - pub async fn new(websocket_url: String, token: String) { + pub async fn new( + websocket_url: String, + token: String, + ) -> Result, tokio_tungstenite::tungstenite::Error> { let parsed_url = Url::parse(&URLBundle::parse_url(websocket_url.clone())).unwrap(); if parsed_url.scheme() != "ws" && parsed_url.scheme() != "wss" { - //return Err(Error::Url(UrlError::UnsupportedUrlScheme)); + return Err(tokio_tungstenite::tungstenite::Error::Url( + UrlError::UnsupportedUrlScheme, + )); } - let payload = GatewayIdentifyPayload { - token: token, - properties: GatewayIdentifyConnectionProps { - os: "any".to_string(), - browser: "chorus-polyphony".to_string(), - device: "chorus-lib".to_string(), - }, - compress: Some(true), - large_threshold: None, - shard: None, - presence: None, - intents: 3276799, + let (ws_stream, _) = match connect_async(websocket_url.clone()).await { + Ok(ws_stream) => ws_stream, + Err(_) => { + return Err(tokio_tungstenite::tungstenite::Error::Io( + io::ErrorKind::ConnectionAborted.into(), + )) + } }; - let payload_string = to_string(&payload).unwrap(); + + return Ok(Gateway { + url: websocket_url, + token, + events: Events::default(), + socket: ws_stream, + }); } } @@ -200,4 +220,11 @@ mod example { Some(err) => println!("You cannot subscribe twice: {}", err), } } + + #[tokio::test] + async fn test_gateway() { + let gateway = Gateway::new("ws://localhost:3001/".to_string(), "none".to_string()) + .await + .unwrap(); + } }