Skip to content

Commit

Permalink
Merge pull request #86 from UkoeHB/upgrade_with_config
Browse files Browse the repository at this point in the history
Axum: separate on_upgrade_with_config from on_upgrade
  • Loading branch information
UkoeHB authored Sep 30, 2023
2 parents 068c0e6 + 308291b commit 7393f98
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Incoming user messages are discarded while a client is reconnecting, to better match the usual behavior of a websocket connection. If you want messages to be buffered while reconnecting, you should implement your own buffer.
- rename `socket::Config` -> `socket::SocketConfig` and add a `heartbeat_ping_msg_fn` member variable in order to support custom Ping/Pong protocols
- add `ClientConfig::socket_config()` setter so clients can define their socket's config
- update `ezsockets::axum::Upgrade::on_upgrade()` to accept a `SocketConfig`
- add `ezsockets::axum::Upgrade::on_upgrade_with_config()` that accepts a `SocketConfig`


Migration guide:
Expand Down Expand Up @@ -66,7 +66,7 @@ async fn websocket_handler(
// before:
ezsocket.on_upgrade(server, session_args) // <----- 5. Remove `session_args` argument
// after:
ezsocket.on_upgrade(server, SocketConfig::default()) // <----- Now you can customize the `Session` inside of `ServerExt::on_connect` via `ezsockets::Request`, and define the socket config here.
ezsocket.on_upgrade(server) // <----- Now you can customize the `Session` inside of `ServerExt::on_connect` via `ezsockets::Request`.
}

// ONLY for `tungstenite`
Expand Down
3 changes: 1 addition & 2 deletions examples/chat-server-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ezsockets::axum::Upgrade;
use ezsockets::CloseFrame;
use ezsockets::Error;
use ezsockets::Server;
use ezsockets::SocketConfig;
use std::collections::HashMap;
use std::io::BufRead;
use std::net::SocketAddr;
Expand Down Expand Up @@ -157,5 +156,5 @@ async fn websocket_handler(
)
.into_response();
}
ezsocket.on_upgrade(server, SocketConfig::default())
ezsocket.on_upgrade(server)
}
14 changes: 12 additions & 2 deletions src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! Extension(server): Extension<ezsockets::Server<EchoServer>>,
//! ezsocket: Upgrade,
//! ) -> impl IntoResponse {
//! ezsocket.on_upgrade(server, ezsockets::SocketConfig::default())
//! ezsocket.on_upgrade(server)
//! }
//! ```
Expand Down Expand Up @@ -170,7 +170,17 @@ impl Upgrade {
/// When using `WebSocketUpgrade`, the response produced by this method
/// should be returned from the handler. See the [module docs](self) for an
/// example.
pub fn on_upgrade<E: ServerExt + 'static>(
pub fn on_upgrade<E: ServerExt + 'static>(self, server: Server<E>) -> Response {
self.on_upgrade_with_config(server, SocketConfig::default())
}

/// Finalize upgrading the connection and call the provided callback with
/// the stream.
///
/// When using `WebSocketUpgrade`, the response produced by this method
/// should be returned from the handler. See the [module docs](self) for an
/// example.
pub fn on_upgrade_with_config<E: ServerExt + 'static>(
self,
server: Server<E>,
socket_config: SocketConfig,
Expand Down
3 changes: 1 addition & 2 deletions tests/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use axum::Router;
use ezsockets::axum::Upgrade;
use ezsockets::Server;
use ezsockets::ServerExt;
use ezsockets::SocketConfig;
use std::net::SocketAddr;

async fn websocket_handler<E>(
Expand All @@ -21,7 +20,7 @@ async fn websocket_handler<E>(
where
E: ServerExt + 'static,
{
ezsocket.on_upgrade(server, SocketConfig::default())
ezsocket.on_upgrade(server)
}

async fn run<E>(create_fn: impl FnOnce(Server<E>) -> E) -> (Server<E>, SocketAddr)
Expand Down

0 comments on commit 7393f98

Please sign in to comment.