refactoring
This commit is contained in:
parent
e235fcc749
commit
87a669f899
@ -20,6 +20,10 @@
|
|||||||
<canvas id="canvas" width="1024" height="1024"></canvas>
|
<canvas id="canvas" width="1024" height="1024"></canvas>
|
||||||
<span class="footer">LJ Sketch</span>
|
<span class="footer">LJ Sketch</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="errorBox" class="invisible"></div>
|
||||||
|
|
||||||
<script src="main.js"></script>
|
<script src="main.js"></script>
|
||||||
|
|
||||||
|
19
pub/main.js
19
pub/main.js
@ -108,13 +108,22 @@ const initDrawing = (canvas, clearButton, selectedColor, ws) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const initWs = errorBox => {
|
||||||
const initWs = () => {
|
|
||||||
const socket = new WebSocket('ws://localhost:3000/ws');
|
const socket = new WebSocket('ws://localhost:3000/ws');
|
||||||
|
|
||||||
socket.addEventListener('open', function (event) {
|
socket.addEventListener('open', function (event) {
|
||||||
|
console.log("Open", event);
|
||||||
|
});
|
||||||
|
socket.addEventListener('error', function (event) {
|
||||||
|
console.log("Error:", event);
|
||||||
|
errorBox.className = "visible";
|
||||||
|
errorBox.innerHTML = "Error: " + event.message;
|
||||||
|
});
|
||||||
|
socket.addEventListener("close", (event) => {
|
||||||
|
console.log("Close:", event);
|
||||||
|
errorBox.className = "visible";
|
||||||
|
errorBox.innerHTML = "Disconnected: server closed connexion";
|
||||||
});
|
});
|
||||||
|
|
||||||
return socket;
|
return socket;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -122,9 +131,9 @@ window.onload = () => {
|
|||||||
const colorsDiv = document.querySelector("#colors");
|
const colorsDiv = document.querySelector("#colors");
|
||||||
const selectedColor = document.querySelector("#selectedColor");
|
const selectedColor = document.querySelector("#selectedColor");
|
||||||
const clearButton = document.querySelector("#clearButton");
|
const clearButton = document.querySelector("#clearButton");
|
||||||
|
const errorBox = document.querySelector("#errorBox");
|
||||||
const canvas = document.querySelector("#canvas");
|
const canvas = document.querySelector("#canvas");
|
||||||
initUI(colorsDiv, selectedColor);
|
initUI(colorsDiv, selectedColor);
|
||||||
let ws = initWs();
|
let ws = initWs(errorBox);
|
||||||
initDrawing(canvas, clearButton, selectedColor, ws);
|
initDrawing(canvas, clearButton, selectedColor, ws);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -64,3 +64,20 @@ body {
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#errorBox {
|
||||||
|
border: thick solid #990000;
|
||||||
|
background: #dd0000;
|
||||||
|
position: fixed;
|
||||||
|
bottom: 0px;
|
||||||
|
right: 0px;
|
||||||
|
text-align: center;
|
||||||
|
padding: 1em;
|
||||||
|
font-size: x-large;
|
||||||
|
}
|
||||||
|
|
||||||
|
.visible {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.invisible {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
@ -25,8 +25,7 @@ async fn gen_server(mut rx: Receiver<GSMsg>) {
|
|||||||
match msg {
|
match msg {
|
||||||
GSMsg::NewClient((addr, c_tx)) => {
|
GSMsg::NewClient((addr, c_tx)) => {
|
||||||
for line in &lines {
|
for line in &lines {
|
||||||
c_tx.send(GSMsg::NewLine(line.clone()))
|
c_tx.send(GSMsg::NewLine(line.clone())).await.unwrap();
|
||||||
.await.unwrap();
|
|
||||||
}
|
}
|
||||||
clients.insert(addr, c_tx);
|
clients.insert(addr, c_tx);
|
||||||
tracing::info!("NewClient {addr}");
|
tracing::info!("NewClient {addr}");
|
||||||
|
@ -27,11 +27,8 @@ const LISTEN_ON : &str = "0.0.0.0:3000";
|
|||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(tracing_subscriber::EnvFilter::try_from_default_env()
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
.unwrap_or_else(|_| "lj_sketch=info,tower_http=info".into()))
|
||||||
.unwrap_or_else(|_| "lj_sketch=info,tower_http=info"
|
|
||||||
.into()),
|
|
||||||
)
|
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use axum::extract::ws::{ Message, Message::Text, Message::Close, WebSocket };
|
use axum::extract::ws::{Message, Message::Text, Message::Close, WebSocket};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::sync::mpsc::{self, Sender};
|
use tokio::sync::mpsc::{self, Sender};
|
||||||
use serde::{Serialize,Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use core::ops::ControlFlow;
|
use core::ops::ControlFlow;
|
||||||
use crate::gen_server::GSMsg;
|
use crate::gen_server::GSMsg;
|
||||||
use crate::line::{Line,simplify_line};
|
use crate::line::{Line, simplify_line};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
#[serde(tag = "t")]
|
#[serde(tag = "t")]
|
||||||
@ -26,8 +26,8 @@ pub async fn handle_socket(
|
|||||||
who: SocketAddr,
|
who: SocketAddr,
|
||||||
gs_tx: Sender<GSMsg>
|
gs_tx: Sender<GSMsg>
|
||||||
) {
|
) {
|
||||||
let (c_tx, mut c_rx) = mpsc::channel(32);
|
let (chan_tx, mut chan_rx) = mpsc::channel(32);
|
||||||
gs_tx.send(GSMsg::NewClient((who, c_tx))).await.unwrap();
|
gs_tx.send(GSMsg::NewClient((who, chan_tx))).await.unwrap();
|
||||||
let mut line : Line = vec![];
|
let mut line : Line = vec![];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -42,7 +42,7 @@ pub async fn handle_socket(
|
|||||||
ControlFlow::Continue(()) => {}
|
ControlFlow::Continue(()) => {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Some(msg) = c_rx.recv() => {
|
Some(msg) = chan_rx.recv() => {
|
||||||
process_gs_msg(&mut socket, &who, msg).await
|
process_gs_msg(&mut socket, &who, msg).await
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
@ -53,7 +53,11 @@ pub async fn handle_socket(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn process_gs_msg(socket: &mut WebSocket, who: &SocketAddr, msg: GSMsg) {
|
async fn process_gs_msg(
|
||||||
|
socket: &mut WebSocket,
|
||||||
|
who: &SocketAddr,
|
||||||
|
msg: GSMsg
|
||||||
|
) {
|
||||||
match msg {
|
match msg {
|
||||||
GSMsg::NewLine(line) => {
|
GSMsg::NewLine(line) => {
|
||||||
socket.send(Message::Text(line_to_json(&line))).await.unwrap();
|
socket.send(Message::Text(line_to_json(&line))).await.unwrap();
|
||||||
@ -75,32 +79,30 @@ async fn process_ws_msg(
|
|||||||
msg: Message
|
msg: Message
|
||||||
) -> ControlFlow<(),()> {
|
) -> ControlFlow<(),()> {
|
||||||
match msg {
|
match msg {
|
||||||
Text(text) => {
|
Text(text) => match serde_json::from_str(&text) {
|
||||||
match serde_json::from_str(&text) {
|
Ok(json) => {
|
||||||
Ok(json) => {
|
tracing::debug!("{who}: '{json:?}'");
|
||||||
tracing::debug!("{who}: '{:?}'", json);
|
match handle_ws_msg(line, json) {
|
||||||
match handle_ws_msg(line, json) {
|
Ok(Some(req)) => gs_tx.send(req).await.unwrap(),
|
||||||
Ok(Some(req)) => gs_tx.send(req).await.unwrap(),
|
Ok(None) => {},
|
||||||
Ok(None) => {},
|
Err(err) => {
|
||||||
Err(err) => {
|
tracing::warn!("{who}: message error: {err}");
|
||||||
tracing::warn!("{who}: message error: {err}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
tracing::warn!("{who}: can't parse JSON: {err}");
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
tracing::warn!("{who}: can't parse JSON: {err}");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Close(close) => {
|
Close(close) => {
|
||||||
tracing::info!("{who}: closing: {:?}", close);
|
tracing::info!("{who}: closing: {close:?}");
|
||||||
gs_tx.send(GSMsg::DeleteClient(*who)).await.unwrap();
|
gs_tx.send(GSMsg::DeleteClient(*who)).await.unwrap();
|
||||||
return ControlFlow::Break(());
|
return ControlFlow::Break(());
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
tracing::warn!("{who}: can't handle message: {:?}", msg);
|
tracing::warn!("{who}: can't handle message: {msg:?}");
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
ControlFlow::Continue(())
|
ControlFlow::Continue(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,13 +123,13 @@ fn handle_ws_msg(
|
|||||||
},
|
},
|
||||||
JMsg::Stroke => {
|
JMsg::Stroke => {
|
||||||
if line.len() > 1 {
|
if line.len() > 1 {
|
||||||
let line2 = simplify_line(line);
|
let simple_line = simplify_line(line);
|
||||||
*line = vec![];
|
*line = vec![];
|
||||||
return Ok(Some(GSMsg::NewLine(line2)));
|
return Ok(Some(GSMsg::NewLine(simple_line)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
JMsg::Line{..} => {
|
JMsg::Line{..} => {
|
||||||
tracing::warn!("recieved a line message O_o");
|
return Err("recieved a line message O_o");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@ -135,9 +137,7 @@ fn handle_ws_msg(
|
|||||||
|
|
||||||
fn line_to_json(line: &Line) -> String {
|
fn line_to_json(line: &Line) -> String {
|
||||||
let line = line.iter()
|
let line = line.iter()
|
||||||
.map(| (x, y, c) | {
|
.map(| (x, y, c) | (*x, *y, format!("#{:06x}", c)))
|
||||||
(*x, *y, format!("#{:06x}", c))
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
serde_json::to_string(&JMsg::Line{ line }).unwrap()
|
serde_json::to_string(&JMsg::Line{ line }).unwrap()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user