-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
96 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,9 @@ Creating a WebSocket server or a client in Rust can be troublesome. This crate f | |
- Traits to allow declarative and event-based programming. | ||
- Easy concurrency with Tokio and async/await. Server sessions are Clone'able and can be shared between tasks. | ||
- Heartbeat mechanism to keep the connection alive. | ||
- Automatic reconnection of WebSocket Client. | ||
- Support for multiple back-ends such as Axum or Tungstenite. | ||
- Automatic reconnection of WebSocket Clients. | ||
- Support for arbitrary client back-ends, with built-in native and WASM client connectors. | ||
- Support for multiple server back-ends such as Axum or Tungstenite. | ||
- TLS support for servers with `rustls` and `native-tls`. | ||
|
||
## Documentation | ||
|
@@ -25,14 +26,14 @@ View the full documentation at [docs.rs/ezsockets](http://docs.rs/ezsockets) | |
|
||
## Client | ||
|
||
[`tokio-tungstenite`](https://github.com/snapview/tokio-tungstenite) is being used under the hood. | ||
By default clients use [`tokio-tungstenite`](https://github.com/snapview/tokio-tungstenite) under the hood. Disable default features and enable `wasm_client` to run clients on WASM targets. | ||
|
||
See [examples/simple-client](https://github.com/gbaranski/ezsockets/tree/master/examples/simple-client) for a simple usage | ||
and [docs.rs/ezsockets/server](https://docs.rs/ezsockets/latest/ezsockets/client/index.html) for documentation. | ||
|
||
## Server | ||
|
||
WebSocket server can use one of supported back-ends: | ||
WebSocket server can use one of the supported back-ends: | ||
- [`tokio-tungstenite`](https://github.com/snapview/tokio-tungstenite) - the simplest way to get started. | ||
- [`axum`](https://github.com/tokio-rs/axum) - ergonomic and modular web framework built with Tokio, Tower, and Hyper | ||
- [`actix-web`](https://github.com/actix/actix-web) - Work in progress at [#22](https://github.com/gbaranski/ezsockets/issues/22) | ||
|
@@ -46,4 +47,4 @@ Licensed under [MIT](https://choosealicense.com/licenses/mit/). | |
|
||
# Contact | ||
|
||
Reach me out on Discord `gbaranski#5119`, or mail me at [email protected]. | ||
Reach me out on Discord `gbaranski#5119`, or mail me at [email protected]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::client::{ClientConfig, ClientConnector}; | ||
|
||
/// Implementation of [`ClientConnector`] for tokio runtimes. | ||
#[derive(Clone)] | ||
pub struct ClientConnectorWasm { | ||
handle: enfync::builtin::wasm::WASMHandle, | ||
} | ||
|
||
impl Default for ClientConnectorWasm { | ||
fn default() -> Self { | ||
let handle = enfync::builtin::wasm::WASMHandle::default(); | ||
Self { handle } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl ClientConnector for ClientConnectorWasm { | ||
type Handle = enfync::builtin::wasm::WASMHandle; | ||
type Message = tokio_tungstenite_wasm::Message; | ||
type WSError = tokio_tungstenite_wasm::Error; | ||
type Socket = tokio_tungstenite_wasm::WebSocketStream; | ||
type ConnectError = tokio_tungstenite_wasm::Error; | ||
|
||
/// Get the connector's runtime handle. | ||
fn handle(&self) -> Self::Handle { | ||
self.handle.clone() | ||
} | ||
|
||
/// Connect to a websocket server. | ||
/// | ||
/// Returns `Err` if the request is invalid. | ||
/// | ||
/// Panics if any headers were added to the client config. Websockets on browser does not support | ||
/// additional headers (use [`ClientConfig::query_parameter()`] instead). | ||
async fn connect(&self, config: &ClientConfig) -> Result<Self::Socket, Self::ConnectError> { | ||
if config.headers().len() > 0 { | ||
panic!("client may not submit HTTP headers in WASM connection requests"); | ||
} | ||
let request_url = config.connect_url(); | ||
let (socket, _) = tokio_tungstenite_wasm::connect(request_url).await?; | ||
Ok(socket) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
#[cfg(feature = "native_client")] | ||
pub mod client_connector_tokio; | ||
cfg_if::cfg_if! { | ||
if #[cfg(all(feature = "native_client", not(target_family = "wasm")))] { | ||
mod client_connector_tokio; | ||
pub use client_connector_tokio::*; | ||
} | ||
} | ||
|
||
//#[cfg(feature = "wasm_client")] | ||
//pub mod client_connector_wasm; | ||
cfg_if::cfg_if! { | ||
if #[cfg(all(feature = "wasm_client", target_family = "wasm"))] { | ||
mod client_connector_wasm; | ||
pub use client_connector_wasm::*; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters