plaza_ws 0.7.0

One client-side WebSocket interface across desktop, browser, and in-process, for frame-loop applications.
Documentation
//! A throwaway echo server, so the `echo` example can be verified without a
//! network or a third-party endpoint.
//!
//! ```sh
//! cargo run -p plaza_ws --features native --example echo_server -- 9001
//! ```
//!
//! Blocking, with one thread per connection, which is all an echo server needs.
//! It is a test fixture; the real server side of plaza is `plaza_session`.

use std::net::TcpListener;
use std::thread;

fn main() {
  let port: u16 = std::env::args().nth(1).and_then(|p| p.parse().ok()).unwrap_or(9001);
  let listener = TcpListener::bind(("127.0.0.1", port)).expect("bind");
  println!("echo server on ws://127.0.0.1:{port}");

  for stream in listener.incoming() {
    let Ok(stream) = stream else { continue };
    thread::spawn(move || {
      let Ok(mut socket) = tungstenite::accept(stream) else { return };
      loop {
        match socket.read() {
          Ok(msg) if msg.is_binary() || msg.is_text() => {
            if socket.send(msg).is_err() {
              return;
            }
          }
          Ok(_) => {}
          Err(_) => return,
        }
      }
    });
  }
}