-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
0c8db8f
commit b159771
Showing
48 changed files
with
3,025 additions
and
904 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,57 +1,72 @@ | ||
use axum::{ | ||
extract::State, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use tracing::instrument; | ||
|
||
use crate::{ | ||
extractors::GameCode, | ||
ports::game_state::GameState, | ||
service::InternalServerError, | ||
}; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[instrument(skip_all, err)] | ||
pub async fn on_connect<G: GameState>( | ||
State(game_state): State<G>, | ||
SessionID(_session_id): SessionID, | ||
GameCode { code }: GameCode, | ||
) -> Result<Response, InternalServerError> { | ||
let pair = game_state.accept_web_socket(code)?; | ||
|
||
for event in game_state.events(code).await?.into_iter() { | ||
pair.server.send(&event)?; | ||
} | ||
mod wasm { | ||
use axum::{extract::State, response::Response}; | ||
use tracing::instrument; | ||
|
||
use crate::{ | ||
extractors::{GameCode, SessionID}, | ||
ports::game_state::GameState, | ||
service::InternalServerError, | ||
}; | ||
|
||
// #[instrument(skip_all, err)] | ||
pub async fn on_connect<G: GameState>( | ||
State(game_state): State<G>, | ||
SessionID(_session_id): SessionID, | ||
GameCode { code }: GameCode, | ||
) -> Result<Response, InternalServerError> { | ||
let pair = game_state.accept_web_socket(code)?; | ||
|
||
let response = Response::builder() | ||
.status(101) | ||
.extension(pair.client) | ||
.body(axum::body::Body::empty()); | ||
for event in game_state.events(code).await?.into_iter() { | ||
pair.server.send(&event)?; | ||
} | ||
|
||
Ok(response?) | ||
let response = Response::builder() | ||
.status(101) | ||
.extension(pair.client) | ||
.body(axum::body::Body::empty()); | ||
|
||
Ok(response?) | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
pub use wasm::on_connect; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[instrument(skip_all, err)] | ||
pub async fn on_connect<G: GameState>( | ||
ws: axum::extract::WebSocketUpgrade, | ||
State(game_state): State<G>, | ||
GameCode { code }: GameCode, | ||
) -> Result<Response, InternalServerError> { | ||
Ok(ws | ||
.on_upgrade(move |mut ws| async move { | ||
let result: anyhow::Result<()> = try { | ||
for event in game_state.events(code).await?.into_iter() { | ||
let message = serde_json::to_string(&event)?; | ||
ws.send(message.into()).await?; | ||
} | ||
mod native { | ||
use axum::{ | ||
extract::State, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use tracing::instrument; | ||
|
||
use crate::{extractors::GameCode, ports::game_state::GameState, service::InternalServerError}; | ||
|
||
game_state.accept_web_socket(code, ws).await?; | ||
}; | ||
#[instrument(skip_all, err)] | ||
pub async fn on_connect<G: GameState>( | ||
ws: axum::extract::WebSocketUpgrade, | ||
State(game_state): State<G>, | ||
GameCode { code }: GameCode, | ||
) -> Result<Response, InternalServerError> { | ||
Ok(ws | ||
.on_upgrade(move |mut ws| async move { | ||
let result: anyhow::Result<()> = try { | ||
for event in game_state.events(code).await?.into_iter() { | ||
let message = serde_json::to_string(&event)?; | ||
ws.send(message.into()).await?; | ||
} | ||
|
||
if let Err(err) = result { | ||
tracing::error!(?err, "error upgrading websocket") | ||
} | ||
}) | ||
.into_response()) | ||
game_state.accept_web_socket(code, ws).await?; | ||
}; | ||
|
||
if let Err(err) = result { | ||
tracing::error!(?err, "error upgrading websocket") | ||
} | ||
}) | ||
.into_response()) | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
pub use native::on_connect; |
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
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
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
Oops, something went wrong.