This commit is contained in:
Edgar 2023-10-02 16:21:02 +02:00
parent d372b650dc
commit 78a11e1995
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
2 changed files with 53 additions and 3 deletions

44
README.md Normal file
View file

@ -0,0 +1,44 @@
# teeobservable
This tool continuously fetches the DDNet master server and observes the changes, broadcasting the events to connected websockets.
By default it listens to `127.0.0.1:3000` and the websocket connection is at `ws://localhost:3000/ws`
Port can be changed with the `PORT` environment variable.
```
> RUST_LOG="teeobserver=debug"
> teeobservable
2023-10-02T14:18:54.933535Z DEBUG teeobserver::util: making request to master
2023-10-02T14:18:55.419999Z DEBUG teeobserver::util: got 1139 servers
2023-10-02T14:18:55.421385Z INFO teeobserver: listening on http://127.0.0.1:3000
2023-10-02T14:18:55.422382Z DEBUG teeobserver::util: making request to master
2023-10-02T14:18:55.525406Z DEBUG teeobserver::util: got 1139 servers
2023-10-02T14:18:55.530730Z INFO teeobserver: sent 0 events to 0 receivers
2023-10-02T14:18:57.627921Z INFO teeobserver::routes: `websocket: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0` at 127.0.0.1:44564 connected.
2023-10-02T14:19:05.422137Z DEBUG teeobserver::util: making request to master
2023-10-02T14:19:05.554810Z DEBUG teeobserver::util: got 1139 servers
2023-10-02T14:19:05.559945Z INFO teeobserver: sent 68 events to 1 receivers
2023-10-02T14:19:15.421549Z DEBUG teeobserver::util: making request to master
2023-10-02T14:19:15.540133Z DEBUG teeobserver::util: got 1139 servers
2023-10-02T14:19:15.545310Z INFO teeobserver: sent 75 events to 1 receivers
```
To connect with a simple webpage:
```html
<html>
<body>
</body>
<script>
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:3000/ws");
// Listen for messages
socket.addEventListener("message", (event) => {
console.log("Message from server ", event.data);
});
</script>
</html>
```

View file

@ -2,7 +2,7 @@ use std::{
collections::{HashSet, VecDeque},
net::SocketAddr,
sync::Arc,
time::{Duration, Instant},
time::Duration,
};
use axum::{routing::get, Router};
@ -10,7 +10,7 @@ use http::Method;
use reqwest::Client;
use sqlx::{postgres::PgPoolOptions, PgPool};
use structures::{MasterEvent, ServerListMap};
use time::{OffsetDateTime, UtcOffset};
use time::OffsetDateTime;
use tokio::{
sync::{
broadcast::{self, Sender},
@ -182,6 +182,12 @@ async fn run() -> anyhow::Result<()> {
}
}
info!(
"sent {} events to {} receivers",
events.len(),
events_sender.receiver_count()
);
let mut lock_events = task_events.write().await;
lock_events.extend(events.drain(..));
let mut servers = task_servers.write().await;
@ -193,7 +199,7 @@ async fn run() -> anyhow::Result<()> {
});
let app = Router::new()
.route("/", get(|| async { "a" }))
.route("/", get(|| async { include_str!("../README.md") }))
.route("/ws", get(routes::ws_handler))
.layer(TraceLayer::new_for_http())
.layer(TimeoutLayer::new(Duration::from_secs(5)))