Skip to content

Commit

Permalink
api: ws traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op committed Mar 25, 2023
1 parent e5398c4 commit 85a3a33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions boltapi/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ pub struct GetEavesdropDataResp {
pub struct TrafficResp {
pub upload: u64,
pub download: u64,
pub upload_speed: Option<u64>,
pub download_speed: Option<u64>,
}
14 changes: 12 additions & 2 deletions boltconn/src/external/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ impl ApiServer {
Json(json!(TrafficResp {
upload: server.stat_center.get_upload().load(Ordering::Relaxed),
download: server.stat_center.get_download().load(Ordering::Relaxed),
upload_speed: None,
download_speed: None,
}))
}

Expand All @@ -152,13 +154,21 @@ impl ApiServer {
}

async fn ws_get_traffic_inner(server: Self, mut socket: WebSocket) {
let mut last_upload = server.stat_center.get_upload().load(Ordering::Relaxed);
let mut last_download = server.stat_center.get_download().load(Ordering::Relaxed);
loop {
// send traffic with 1 second interval
let upload = server.stat_center.get_upload().load(Ordering::Relaxed);
let download = server.stat_center.get_download().load(Ordering::Relaxed);
let data = json!(TrafficResp {
upload: server.stat_center.get_upload().load(Ordering::Relaxed),
download: server.stat_center.get_download().load(Ordering::Relaxed),
upload,
download,
upload_speed: Some(upload - last_upload),
download_speed: Some(download - last_download)
})
.to_string();
last_upload = upload;
last_download = download;
if socket.send(Message::Text(data)).await.is_err() {
return;
}
Expand Down

0 comments on commit 85a3a33

Please sign in to comment.