server/
server.rs

1use std::{io, string::FromUtf8Error};
2
3use tubes::prelude::*;
4
5pub struct Msg(pub String);
6
7impl From<String> for Msg {
8    fn from(value: String) -> Self {
9        Self(value)
10    }
11}
12
13impl TryFrom<&[u8]> for Msg {
14    type Error = FromUtf8Error;
15
16    fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
17        Ok(Msg(String::from_utf8(value.to_vec())?))
18    }
19}
20
21impl TryFrom<Msg> for Vec<u8> {
22    type Error = ();
23
24    fn try_from(value: Msg) -> std::result::Result<Self, Self::Error> {
25        Ok(value.0.into())
26    }
27}
28
29fn main() {
30    let mut s = Session::new_server(":5000".into());
31    s.start().unwrap();
32
33    let mut line = String::new();
34    loop {
35        io::stdin().read_line(&mut line).unwrap();
36        s.broadcast(line.clone().into()).unwrap();
37        line.clear();
38    }
39}