pub struct Ws { /* private fields */ }
Expand description
Extracted by the ws
filter, and used to finish an upgrade.
Implementations
sourceimpl Ws
impl Ws
sourcepub fn on_upgrade<F, U>(self, func: F) -> impl Reply where
F: FnOnce(WebSocket) -> U + Send + 'static,
U: Future<Output = ()> + Send + 'static,
pub fn on_upgrade<F, U>(self, func: F) -> impl Reply where
F: FnOnce(WebSocket) -> U + Send + 'static,
U: Future<Output = ()> + Send + 'static,
Finish the upgrade, passing a function to handle the WebSocket
.
The passed function must return a Future
.
Examples found in repository?
examples/websockets.rs (lines 15-23)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
async fn main() {
pretty_env_logger::init();
let routes = warp::path("echo")
// The `ws()` filter will prepare the Websocket handshake.
.and(warp::ws())
.map(|ws: warp::ws::Ws| {
// And then our closure will be called when it completes...
ws.on_upgrade(|websocket| {
// Just echo all messages back...
let (tx, rx) = websocket.split();
rx.forward(tx).map(|result| {
if let Err(e) = result {
eprintln!("websocket error: {:?}", e);
}
})
})
});
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
More examples
examples/websockets_chat.rs (line 40)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
async fn main() {
pretty_env_logger::init();
// Keep track of all connected users, key is usize, value
// is a websocket sender.
let users = Users::default();
// Turn our "state" into a new Filter...
let users = warp::any().map(move || users.clone());
// GET /chat -> websocket upgrade
let chat = warp::path("chat")
// The `ws()` filter will prepare Websocket handshake...
.and(warp::ws())
.and(users)
.map(|ws: warp::ws::Ws, users| {
// This will call our function if the handshake succeeds.
ws.on_upgrade(move |socket| user_connected(socket, users))
});
// GET / -> index html
let index = warp::path::end().map(|| warp::reply::html(INDEX_HTML));
let routes = index.or(chat);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
sourcepub fn max_send_queue(self, max: usize) -> Self
pub fn max_send_queue(self, max: usize) -> Self
Set the size of the internal message send queue.
sourcepub fn max_message_size(self, max: usize) -> Self
pub fn max_message_size(self, max: usize) -> Self
Set the maximum message size (defaults to 64 megabytes)
sourcepub fn max_frame_size(self, max: usize) -> Self
pub fn max_frame_size(self, max: usize) -> Self
Set the maximum frame size (defaults to 16 megabytes)
Trait Implementations
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self