#![cfg(feature = "__rt_native__")]
pub use mews::{
CloseCode, CloseFrame, Config, Connection, Message, ReadHalf, WriteHalf, connection, split,
};
pub(crate) type Session = mews::WebSocket<crate::session::Connection>;
impl<'ctx> super::WebSocketContext<'ctx> {
pub fn upgrade<H, F>(self, handler: H) -> WebSocket
where
H: FnOnce(Connection<crate::session::Connection>) -> F + Send + Sync + 'static,
F: std::future::Future<Output = ()> + Send + 'static,
{
self.upgrade_with(Config::default(), handler)
}
pub fn upgrade_with<H, F>(self, config: Config, handler: H) -> WebSocket
where
H: FnOnce(Connection<crate::session::Connection>) -> F + Send + Sync + 'static,
F: std::future::Future<Output = ()> + Send + 'static,
{
let (sign, session) = mews::WebSocketContext::new(self.sec_websocket_key)
.with(config)
.on_upgrade(handler);
WebSocket { sign, session }
}
}
pub struct WebSocket {
sign: String,
session: Session,
}
impl crate::IntoResponse for WebSocket {
fn into_response(self) -> crate::Response {
let mut res = crate::Response::SwitchingProtocols();
res.content = crate::response::Content::WebSocket(self.session);
res.with_headers(|h| {
h.connection("Upgrade")
.upgrade("websocket")
.sec_websocket_accept(self.sign)
})
}
#[cfg(feature = "openapi")]
fn openapi_responses() -> crate::openapi::Responses {
crate::openapi::Responses::new([(
101,
crate::openapi::Response::when("Upgrade to WebSocket"),
)])
}
}