pub struct CloseHandle { /* private fields */ }server only.Expand description
Closes a running Connection from outside it,
for example when the credentials of the opening handshake expire (RFC
8887 §4.2). Get one with
Connection::close_handle before
calling run; clone it freely and send it
to another task.
close makes run send a Close frame with the given
code and reason, run the same shutdown as any other end (push
subscription dropped, extension queue closed,
ExtensionHandler::close called),
and return Ok(()). run does not wait for the client’s answering
Close; the transport (for example
tungstenite::serve) drops the socket.
Nothing queued is sent ahead of the Close: Responses of Requests still
running, queued extension messages and push notifications not yet
written are dropped. A message run is writing when close is called
is finished first. After credentials expire this is what the drafts
ask: “A server that does not close the connection when credentials
expire SHOULD NOT deliver further ephemeral events on that connection”
(draft-atwood-jmap-chat-wss-00).
use std::sync::Arc;
use std::time::Duration;
use jmap_websocket::jmap_server::Dispatcher;
use jmap_websocket::jmap_types::State;
use jmap_websocket::server::{CloseCode, Connection, DispatcherHandler};
let handler = Arc::new(DispatcherHandler::new(dispatcher, (), || State::from("s0")));
let connection = Connection::new(handler);
let close = connection.close_handle();
// The credentials of the opening handshake expire after `expires_in`.
tokio::spawn(async move {
tokio::time::sleep(expires_in).await;
close.close(CloseCode::POLICY_VIOLATION, "credentials expired");
});
jmap_websocket::tungstenite::serve(connection, ws).awaitImplementations§
Source§impl CloseHandle
impl CloseHandle
Sourcepub fn close(&self, code: CloseCode, reason: impl Into<String>) -> bool
pub fn close(&self, code: CloseCode, reason: impl Into<String>) -> bool
Ask the connection to close with code and reason.
reason longer than MAX_CLOSE_REASON_LEN bytes is cut to the
longest prefix within it that ends on a character boundary, so the
Close frame stays a valid control frame (RFC 6455 §5.5).
Returns true when this call is the request that run acts on.
Returns false, and does nothing, when an earlier close (through
any clone) asked already (the first request wins), or run has
returned (the client closed, the stream ended, or it failed).
Called before run starts, the Close is the first thing run
sends. true is not a promise of delivery: the client may close,
or the socket fail, at the same moment.