Bevy Simplenet
Provides a bi-directional server/client channel implemented over websockets. This crate is suitable for user authentication, talking to a matchmaking service, communicating between micro-services, games that don't have strict latency requirements, etc.
- Client/server channel includes one-shot messages and a request/response API.
- Client message status tracking.
- Clients automatically work on native and WASM targets.
- Client authentication (WIP).
- Optional server TLS.
Check out the example for a demonstration of how to build a Bevy client using this crate.
Features
default:bevy,client,serverbevy: derivesResourceon [Client] and [Server]client: enables clients (native and WASM targets)server: enables servers (native-only targets)tls-rustls: enables TLS for servers viarustlstls-openssl: enables TLS for servers viaOpenSSL
WASM
On WASM targets the client backend will not update while any other tasks are running. You must either build an IO-oriented application that naturally spends a lot of time polling tasks, or manually release the main thread periodically (e.g. with web_sys::Window::set_timeout_with_callback_and_timeout_and_arguments_0()). For Bevy apps the latter happens automatically at the end of every app update/tick (see the bevy::app::ScheduleRunnerPlugin implementation).
Usage notes
- Servers and clients must be created with enfync runtimes. The backend is ezsockets.
- A client's [
AuthRequest] type must match the corresponding server's [Authenticator] type. - Client ids are defined by clients via their [
AuthRequest] when connecting to a server. This means multiple sessions from the same client will have the same session id. Connections will be rejected if an id is already connected. - Client connect messages will be cloned for all reconnect attempts, so they should be treated as static data.
- Server or client messages may fail to send if the underlying connection is broken. Clients can use the signals returned from [
Client::send()] and [Client::request()] to track the status of a message. Message tracking is not currently available for servers. - Tracing levels assume the server is trusted and clients are not trusted.
Example
// path shortcuts
use ;
use Arc;
use sleep;
use Duration;
// define a channel
;
;
;
;
type TestServer = Server;
type TestClient = Client;
type TestServerVal = ServerValFrom;
type TestClientVal = ClientValFrom;
// enable tracing (with crate `tracing-subscriber`)
/*
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
tracing::info!("README test start");
*/
// make a server
let server = server_factory.new_server;
assert_eq!;
// sleep duration for async machinery
let sleep_duration = from_millis;
// make a client
let client_id = 0u128;
let client = client_factory.new_client;
sleep;
assert_eq!;
// read connection messages
let Connected =
server.next_report.unwrap else ;
let Connected =
client.next_report.unwrap else ;
assert_eq!;
assert_eq!;
// send message: client -> server
let signal = client.send.unwrap;
assert_eq!;
sleep;
assert_eq!;
// read message from client
let = server.next_val.unwrap
else ;
assert_eq!;
assert_eq!;
// send message: server -> client
server.send.unwrap;
sleep;
// read message from server
let Msg = client.next_val.unwrap
else ;
assert_eq!;
// send request to server
let signal = client.request.unwrap;
assert_eq!;
sleep;
assert_eq!;
// read request from client
let = server.next_val.unwrap
else ;
// acknowledge the request (consumes the token without sending a Response)
server.acknowledge.unwrap;
sleep;
assert_eq!;
// read acknowledgement from server
let Ack = client.next_val.unwrap
else ;
assert_eq!;
// client closes itself
client.close;
sleep;
assert_eq!;
// read disconnection messages
let Disconnected = server.next_report.unwrap
else ;
let ClosedBySelf = client.next_report.unwrap
else ;
let IsDead = client.next_report.unwrap
else ;
TODOs
- This crate causes linker errors when the
bevy/dynamic_linkingfeature is enabled. - Implement
AuthTokenfor client/server authentication. - Use const generics to bake protocol versions into
ServerandClientdirectly, instead of relying on factories (currently blocked by lack of robust compiler support). - Message status tracking for server messages. This may require changes to
ezsocketsin order to inject aMessageSignalinsantiated in thebevy_simplenet::Server::send()method.
Bevy compatability
| bevy | bevy_simplenet |
|---|---|
| 0.11 | master |