feat : 内容序列化
This commit is contained in:
@@ -7,5 +7,7 @@ edition = "2024"
|
||||
env_logger = "0.11.8"
|
||||
futures = "0.3.31"
|
||||
log = "0.4.27"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.133"
|
||||
tokio = { version = "1.47.0", features = ["full"] }
|
||||
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
mod utils;
|
||||
mod model;
|
||||
|
||||
use futures::StreamExt;
|
||||
use log::info;
|
||||
use serde_json;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use utils::logger;
|
||||
use crate::model::ws::WsMessage;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -16,7 +19,26 @@ async fn main() {
|
||||
|
||||
let (_, mut read) = stream.split();
|
||||
while let Some(msg) = read.next().await {
|
||||
info!("Received message: {msg:?}");
|
||||
match msg {
|
||||
Ok(tokio_tungstenite::tungstenite::Message::Text(text)) => {
|
||||
info!("Received text message: {}", text);
|
||||
match serde_json::from_str::<WsMessage>(&text) {
|
||||
Ok(ws_msg) => {
|
||||
info!("Parsed message: {ws_msg:?}");
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Failed to parse message: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(other_msg) => {
|
||||
info!("Received non-text message: {other_msg:?}");
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Error receiving message: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
1
packages/gotify-ws/src/model/mod.rs
Normal file
1
packages/gotify-ws/src/model/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod ws;
|
||||
9
packages/gotify-ws/src/model/ws.rs
Normal file
9
packages/gotify-ws/src/model/ws.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct WsMessage {
|
||||
id: u64,
|
||||
appid: u64,
|
||||
message: String,
|
||||
title: String,
|
||||
priority: u64,
|
||||
date: String,
|
||||
}
|
||||
Reference in New Issue
Block a user