pub struct WebSocket { /* private fields */ }
Available on crate feature
websocket
only.Expand description
Implementations
sourceimpl WebSocket
impl WebSocket
sourcepub fn protocols<I>(self, protocols: I) -> Selfwhere
I: IntoIterator,
I::Item: Into<Cow<'static, str>>,
pub fn protocols<I>(self, protocols: I) -> Selfwhere
I: IntoIterator,
I::Item: Into<Cow<'static, str>>,
Set the known protocols.
If the protocol name specified by Sec-WebSocket-Protocol
header
to match any of them, the upgrade response will include
Sec-WebSocket-Protocol
header and return the protocol name.
use futures_util::{SinkExt, StreamExt};
use poem::{get, handler, web::websocket::WebSocket, IntoResponse, Route};
#[handler]
async fn index(ws: WebSocket) -> impl IntoResponse {
ws.protocols(vec!["graphql-rs", "graphql-transport-ws"])
.on_upgrade(|socket| async move {
// ...
})
}
let app = Route::new().at("/", get(index));
sourcepub fn on_upgrade<F, Fut>(self, callback: F) -> WebSocketUpgraded<F>where
F: FnOnce(WebSocketStream) -> Fut + Send + Sync + 'static,
Fut: Future + Send + 'static,
pub fn on_upgrade<F, Fut>(self, callback: F) -> WebSocketUpgraded<F>where
F: FnOnce(WebSocketStream) -> Fut + Send + Sync + 'static,
Fut: Future + Send + 'static,
Finalize upgrading the connection and call the provided callback
with
the stream.
Note that the return value of this function must be returned from the handler.
Trait Implementations
sourceimpl<'a> FromRequest<'a> for WebSocket
impl<'a> FromRequest<'a> for WebSocket
sourcefn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Extract from request head and body.
Auto Trait Implementations
impl !RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl !UnwindSafe for WebSocket
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more