pub struct WebSocketBridge { /* private fields */ }Expand description
Accepts panels, broadcasts events to them, and collects their commands.
Built on the engine’s thread and used from there; the server runs elsewhere and shares nothing but data.
Implementations§
Source§impl WebSocketBridge
impl WebSocketBridge
Sourcepub fn bind(address: &str) -> Result<Self, Error>
pub fn bind(address: &str) -> Result<Self, Error>
Binds a listener and starts serving in the background.
The socket is bound synchronously, so a port already in use is reported to the caller that can do something about it rather than disappearing into a thread.
§Errors
Error::Transport if the address cannot be bound, or
Error::ThreadNotStarted if the server thread cannot be created.
Sourcepub fn bind_with_page(
address: &str,
page: Option<String>,
) -> Result<Self, Error>
pub fn bind_with_page( address: &str, page: Option<String>, ) -> Result<Self, Error>
Binds, and also serves page to a browser asking for the root.
One address for both, so opening the host’s address is the whole setup
and the panel need not be found on disk. It also gives the page and the
socket the same origin, which is what makes an origin check meaningful;
a page loaded from a file has an origin of null.
A browser request never consumes a connection slot or a subscription: it is answered and closed before either is taken.
§Errors
Error::Transport if the address cannot be bound, or
Error::ThreadNotStarted if the server thread will not start.
Sourcepub fn bind_with(address: &str, options: Options) -> Result<Self, Error>
pub fn bind_with(address: &str, options: Options) -> Result<Self, Error>
Binds with everything the host wants to decide up front.
The way to reach the origin allowlist. See Options.
§Errors
Error::Transport if the address cannot be bound, or
Error::ThreadNotStarted if the server thread will not start.
Sourcepub const fn address(&self) -> SocketAddr
pub const fn address(&self) -> SocketAddr
The address actually bound, which resolves a port of 0.
Sourcepub fn client_count(&self) -> usize
pub fn client_count(&self) -> usize
How many panels are connected.
Sourcepub fn publish(&self, event: &MessageEvent)
pub fn publish(&self, event: &MessageEvent)
Sends an event to every connected panel.
Never blocks and never fails for want of an audience: with nobody connected the event is dropped, because a station must not stop testing because no one is watching.
Sourcepub fn reply(&self, request: &Request, response: &Response)
pub fn reply(&self, request: &Request, response: &Response)
Answers one request, addressed to the panel that made it.
Sourcepub fn next_command(&self) -> Option<Request>
pub fn next_command(&self) -> Option<Request>
Takes the next command, if one is waiting.
Non-blocking on purpose. The engine thread has its own queue to pump and cannot afford to wait here; it drains what has arrived and gets on with the run.