use futures_util::{SinkExt, StreamExt};
use solid_pod_rs::notifications::ChangeNotification;
use tokio_tungstenite::tungstenite::Message;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = std::env::args().skip(1);
let ws_url = args
.next()
.unwrap_or_else(|| "ws://127.0.0.1:8765/.notifications/websocket".to_string());
let topic = args.next().unwrap_or_else(|| "/".to_string());
println!("connecting to {ws_url}");
let (mut ws, _response) = tokio_tungstenite::connect_async(&ws_url).await?;
let subscribe = serde_json::json!({
"@context": "https://www.w3.org/ns/solid/notifications-context/v1",
"type": "subscribe",
"topic": topic,
});
ws.send(Message::Text(subscribe.to_string())).await?;
println!("subscribed to topic: {topic}");
while let Some(msg) = ws.next().await {
match msg? {
Message::Text(text) => {
match serde_json::from_str::<ChangeNotification>(&text) {
Ok(note) => {
let pretty = serde_json::to_string_pretty(¬e)
.unwrap_or_else(|_| text.clone());
println!("{pretty}");
}
Err(_) => {
println!("raw frame: {text}");
}
}
}
Message::Close(frame) => {
println!("server closed connection: {frame:?}");
break;
}
Message::Ping(data) => {
ws.send(Message::Pong(data)).await?;
}
_ => {}
}
}
Ok(())
}