Skip to content

Commit

Permalink
feat: connect to server correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sysnote8main committed Jan 28, 2025
1 parent a2d12d4 commit 5338576
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
anyhow = "1.0.95"
arc-swap = "=1.7.1"
axum = "0.8.2"
base64 = "0.22.1"
bytes = "1.9.0"
cfg-if = "=1.0.0"
dotenvy = "=0.15.7"
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use arc_swap::ArcSwap;
use axum::{body::Bytes, http::Request};
use fastwebsockets::{FragmentCollector, Frame, OpCode};
use http_body_util::Empty;
use hyper::{header::{AUTHORIZATION, CONNECTION, UPGRADE}, upgrade::Upgraded};
use hyper::{header::{AUTHORIZATION, CONNECTION, HOST, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_VERSION, UPGRADE}, upgrade::Upgraded};
use hyper_util::rt::TokioIo;
use packet::PacketData;
use base64::{prelude::BASE64_STANDARD, Engine};
use rust_socketio::{ClientBuilder, Event, Payload, RawClient};
use self_update::cargo_crate_version;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -121,14 +122,14 @@ async fn connect(pass: String) -> Result<FragmentCollector<TokioIo<Upgraded>>> {
.method("GET")
.uri(format!("http://localhost:3000/server"))
.header(AUTHORIZATION, pass)
.header("Host", "localhost:3000")
.header(HOST, "localhost:3000")
.header(UPGRADE, "websocket")
.header(CONNECTION, "upgrade")
.header(
"Sec-WebSocket-Key",
SEC_WEBSOCKET_KEY,
fastwebsockets::handshake::generate_key(),
)
.header("Sec-WebSocket-Version", "13")
.header(SEC_WEBSOCKET_VERSION, "13")
.body(Empty::<Bytes>::new())?;

let (ws, _) = fastwebsockets::handshake::client(&SpawnExecutor, req, stream).await?;
Expand All @@ -150,7 +151,7 @@ pub async fn start() {
Ok(t) => {
match t {
PacketData::Sync(_) => {
let status = json!(SystemStatus::get(&sys));
let status = json!(PacketData::Sync(Option::Some(SystemStatus::get(&sys))));
let status_bytes = serde_json::to_string(&status).expect("Failed to serialize to json");
// let status_bytes = format!("{:?}", status);
if let Err(e) = _ws.write_frame(Frame::text(fastwebsockets::Payload::Borrowed(status_bytes.as_bytes()))).await {
Expand Down
4 changes: 3 additions & 1 deletion src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{self, Deserialize, Serialize};

use crate::status::SystemStatus;

#[derive(Serialize, Deserialize)]
#[serde(tag = "event_type", content = "data")]
pub enum PacketData {
#[serde(rename = "SYNC")]
Sync(String),
Sync(Option<SystemStatus>),
#[serde(rename = "STATUS")]
Status(String)
}

0 comments on commit 5338576

Please sign in to comment.