Skip to content

Commit

Permalink
fix: websocket error
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhazhazhu committed Jan 24, 2024
1 parent 2a0902d commit 59c1efc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ jobs:

- name: Install musl(linux)
if: matrix.build == 'linux'
run: sudo apt-get install -y musl-tools
run:
- sudo apt-get install -y musl-tools
- sudo apt-get install -y libssl-dev

- name: Build binary
run: cargo build --verbose --release --target ${{ matrix.target }} --package airnc
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: sudo apt-get install -y musl-tools

- name: Build binary
run: cargo build --verbose --release
run: cargo build --verbose --release --package airnc-server
env:
RUST_BACKTRACE: 1

Expand Down
10 changes: 4 additions & 6 deletions crates/cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ pub async fn run_server(cli: Cli) -> Result<(), std::io::Error> {
};

let service_disable = cli.config.service_disable.clone();
let ws_task = task::spawn(async move {
if !service_disable {
if !service_disable {
task::spawn(async move {
connect_and_handle_messages(service).await;
}
});
});
}

HttpServer::new(move || {
App::new()
Expand All @@ -67,8 +67,6 @@ pub async fn run_server(cli: Cli) -> Result<(), std::io::Error> {
.run()
.await?;

ws_task.await?;

Ok(())
}

Expand Down
42 changes: 17 additions & 25 deletions crates/cli/src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::process;
use tokio::{
select,
signal::unix::{signal, SignalKind},
};
use websockets::{Frame, WebSocket};

#[derive(Deserialize, Serialize, Debug)]
Expand All @@ -13,37 +9,33 @@ pub struct Service {
}

pub async fn connect_and_handle_messages(service: Service) {
println!("Ws: server connecting...");
match WebSocket::connect("ws://120.55.189.199:8080/ws").await {
Ok(mut ws) => {
println!("{}", "Ws: server connect success".green());
let value = serde_json::to_string(&service).unwrap();
ws.send_text(value).await.unwrap();
let mut signal = signal(SignalKind::interrupt()).unwrap();

loop {
select! {
_ = signal.recv() => {
process::exit(1);
}
msg = ws.receive() => {
let msg = ws.receive().await;
match msg {
Ok(msg) => {
match msg {
Ok(msg) => {
match msg {
Frame::Text { payload, .. } => {
// 处理接收到的文本消息
println!("➤ Received: {}", payload);
}
Frame::Close { .. } => {
println!("➤ Received close");
break;
}
_ => {}
}
Frame::Text { payload, .. } => {
// 处理接收到的文本消息
println!("➤ Received: {}", payload);
}
Err(err) => {
println!("websocket connect fail: {}", err);
Frame::Close { .. } => {
println!("➤ Received close");
break;
}
_ => {}
}
}
Err(err) => {
println!("websocket connect fail: {}", err);
break;
}
}
}
}
Expand Down

0 comments on commit 59c1efc

Please sign in to comment.