implement Gateway::new()

This commit is contained in:
bitfl0wer 2023-04-28 23:21:55 +02:00
parent 34ba59d0e5
commit f565cf8366
2 changed files with 47 additions and 19 deletions

View File

@ -15,3 +15,4 @@ regex = "1.7.3"
custom_error = "1.9.2" custom_error = "1.9.2"
native-tls = "0.2.11" native-tls = "0.2.11"
tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]} tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]}
futures-util = "0.3.28"

View File

@ -1,16 +1,24 @@
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::time::Duration;
use crate::api::types::*; use crate::api::types::*;
use crate::api::WebSocketEvent; use crate::api::WebSocketEvent;
use crate::errors::ObserverError; use crate::errors::ObserverError;
use crate::gateway::events::Events; use crate::gateway::events::Events;
use crate::URLBundle; use crate::URLBundle;
use futures_util::SinkExt;
use futures_util::StreamExt;
use reqwest::Url; use reqwest::Url;
use serde::Deserialize;
use serde::Serialize;
use serde_json::from_str;
use serde_json::to_string; use serde_json::to_string;
use tokio::io;
use tokio::net::TcpStream; 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::MaybeTlsStream;
use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::WebSocketStream;
@ -21,31 +29,43 @@ implemented [Types] with the trait [`WebSocketEvent`]
*/ */
pub struct Gateway<'a> { pub struct Gateway<'a> {
pub url: String, pub url: String,
pub token: String,
pub events: Events<'a>, pub events: Events<'a>,
socket: Arc<Mutex<Option<WebSocketStream<MaybeTlsStream<TcpStream>>>>>, socket: WebSocketStream<MaybeTlsStream<TcpStream>>,
thread_handle: Option<JoinHandle<()>>, }
#[derive(Deserialize, Serialize, Debug)]
enum GatewayIntervalEvent {
Heartbeat(Option<u64>),
Hello(u64),
} }
impl<'a> Gateway<'a> { impl<'a> Gateway<'a> {
pub async fn new(websocket_url: String, token: String) { pub async fn new(
websocket_url: String,
token: String,
) -> Result<Gateway<'a>, tokio_tungstenite::tungstenite::Error> {
let parsed_url = Url::parse(&URLBundle::parse_url(websocket_url.clone())).unwrap(); let parsed_url = Url::parse(&URLBundle::parse_url(websocket_url.clone())).unwrap();
if parsed_url.scheme() != "ws" && parsed_url.scheme() != "wss" { 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 { let (ws_stream, _) = match connect_async(websocket_url.clone()).await {
token: token, Ok(ws_stream) => ws_stream,
properties: GatewayIdentifyConnectionProps { Err(_) => {
os: "any".to_string(), return Err(tokio_tungstenite::tungstenite::Error::Io(
browser: "chorus-polyphony".to_string(), io::ErrorKind::ConnectionAborted.into(),
device: "chorus-lib".to_string(), ))
}, }
compress: Some(true),
large_threshold: None,
shard: None,
presence: None,
intents: 3276799,
}; };
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), 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();
}
} }