This commit is contained in:
jane 2021-12-28 20:20:48 -05:00
parent 3b18e46410
commit 30e8e476b9

View file

@ -1,9 +1,9 @@
use std::net::TcpListener; use std::net::TcpListener;
use std::thread; use std::thread;
use tungstenite::accept; use tungstenite::accept;
use util::pattern::*; use crate::util::pattern;
pub fn websocket(pattern: &mut Vec<Box<dyn Pattern>>) { pub fn websocket(pattern: &mut Vec<Box<dyn pattern::Pattern>>) {
let server = TcpListener::bind("0.0.0.0:29999").unwrap(); let server = TcpListener::bind("0.0.0.0:29999").unwrap();
for stream in server.incoming() { for stream in server.incoming() {
thread::spawn (move || { thread::spawn (move || {
@ -11,17 +11,18 @@ pub fn websocket(pattern: &mut Vec<Box<dyn Pattern>>) {
loop { loop {
let input = websocket.read_message().unwrap(); let input = websocket.read_message().unwrap();
if input.is_text() { if input.is_text() {
let mul_string = input.to_text().unwrap_or("unit"); let string = input.to_text().unwrap_or("unit");
match mul_string.iter().cloned().map(parse_line).collect() { let mul_string = pattern::format_multiline(string);
match mul_string.iter().cloned().map(pattern::parse_line).collect() {
Ok(patterns) => { Ok(patterns) => {
pattern = patterns; *pattern = patterns;
let text = format!("Ok!\n\n{}", mul_string); let text = format!("Ok!\n\n{}", string);
let msg = tungstenite::Message::from(text); let msg = tungstenite::Message::from(text);
websocket.write_message(msg).unwrap(); websocket.write_message(msg).unwrap();
} }
Err(err) => { Err(err) => {
println!("ret err {:?}", err); println!("ret err {:?}", err);
let msg = tungstenite::Message::from(err); let msg = tungstenite::Message::from(err.to_string());
websocket.write_message(msg).unwrap(); websocket.write_message(msg).unwrap();
} }
} }
@ -33,4 +34,4 @@ pub fn websocket(pattern: &mut Vec<Box<dyn Pattern>>) {
} }
}); });
} }
} }