pub struct Connection { /* private fields */ }Expand description
A running connection.
Dropping it asks the thread to stop after its current wait.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn open(
stream_url: &str,
token: Option<String>,
trust: &Trust,
wake: impl Fn() + Send + 'static,
) -> Self
pub fn open( stream_url: &str, token: Option<String>, trust: &Trust, wake: impl Fn() + Send + 'static, ) -> Self
Opens the stream and keeps it open, reconnecting for as long as the connection lives.
token is optional, but a server with security on and allowReadonly
off will open this stream, greet the client, and then send it nothing
– see crate::access. Passing None is therefore a choice to be
made knowingly, not a default to fall back on.
trust decides which server may answer a wss:// URL. It is asked
for on every connection rather than defaulted, because the failure it
prevents is silent: a client that trusted anything would reconnect
happily to whatever took the boat’s address and hand it the token.
wake is called from this connection’s own thread once something
is worth draining – see the module doc. A consumer with nothing
better to do than poll on a fixed timer may pass || {} and get
exactly today’s behaviour; passing the real thing is what lets the
timer go away.
Sourcepub fn publish(&self, delta_json: String) -> Result<(), ClientError>
pub fn publish(&self, delta_json: String) -> Result<(), ClientError>
Queues delta_json to be sent on the wire, as-is – building a
well-formed delta is the caller’s own job (see
[signalk::notification::mob_delta] for the one this exists for
today). One-way: an ordinary delta carries no reply for this to
wait on, unlike crate::resources::Client’s own writes.
§Errors
If the background thread this would be sent from is already gone – the connection was dropped, or never opened.
Sourcepub fn drain(&self) -> impl Iterator<Item = Event> + '_
pub fn drain(&self) -> impl Iterator<Item = Event> + '_
Whatever has arrived, without waiting.
Drained on the consumer’s own schedule rather than handed over
message by message: the consumer decides when it is ready to draw,
which is what keeps a burst of deltas from turning into a burst of
redraws. wake (see the module doc) is what tells it when that
schedule should run sooner than usual; this is still the only way
the events themselves are read.
Marked not-pending before the drain, not after: a message arriving during this call must still cause a future wake, and clearing first means the worst case is one redundant wake, never a missed one.