Struct matchbox_socket::WebRtcSocket
source · pub struct WebRtcSocket<C: ChannelPlurality = SingleChannel> { /* private fields */ }
Expand description
Contains a set of WebRtcChannel
s and connection metadata.
Implementations§
source§impl WebRtcSocket
impl WebRtcSocket
sourcepub fn builder(room_url: impl Into<String>) -> WebRtcSocketBuilder
pub fn builder(room_url: impl Into<String>) -> WebRtcSocketBuilder
Creates a new builder for a connection to a given room with a given number of re-connection attempts.
You must add at least one channel with WebRtcSocketBuilder::add_channel
before you can build the WebRtcSocket
sourcepub fn new_unreliable(
room_url: impl Into<String>
) -> (WebRtcSocket<SingleChannel>, MessageLoopFuture)
pub fn new_unreliable( room_url: impl Into<String> ) -> (WebRtcSocket<SingleChannel>, MessageLoopFuture)
Creates a WebRtcSocket
and the corresponding MessageLoopFuture
for a
socket with a single unreliable channel.
The returned MessageLoopFuture
should be awaited in order for messages to
be sent and received.
Please use the WebRtcSocketBuilder
to create non-trivial sockets.
sourcepub fn new_reliable(
room_url: impl Into<String>
) -> (WebRtcSocket<SingleChannel>, MessageLoopFuture)
pub fn new_reliable( room_url: impl Into<String> ) -> (WebRtcSocket<SingleChannel>, MessageLoopFuture)
Creates a WebRtcSocket
and the corresponding MessageLoopFuture
for a
socket with a single reliable channel.
The returned MessageLoopFuture
should be awaited in order for messages to
be sent and received.
Please use the WebRtcSocketBuilder
to create non-trivial sockets.
source§impl<C: ChannelPlurality> WebRtcSocket<C>
impl<C: ChannelPlurality> WebRtcSocket<C>
sourcepub fn update_peers(&mut self) -> Vec<(PeerId, PeerState)>
pub fn update_peers(&mut self) -> Vec<(PeerId, PeerState)>
Handle peers connecting or disconnecting
Constructed using WebRtcSocketBuilder
.
Update the set of peers used by WebRtcSocket::connected_peers
and
WebRtcSocket::disconnected_peers
.
Returns the peers that connected or disconnected since the last time this method was called.
See also: PeerState
§Panics
Will panic if the socket future has been dropped.
WebRtcSocket::try_update_peers
is the equivalent method that will instead return a
Result
.
sourcepub fn try_update_peers(
&mut self
) -> Result<Vec<(PeerId, PeerState)>, ChannelError>
pub fn try_update_peers( &mut self ) -> Result<Vec<(PeerId, PeerState)>, ChannelError>
Similar to WebRtcSocket::update_peers
. Will instead return a Result::Err if the
socket is closed.
sourcepub fn connected_peers(&self) -> impl Iterator<Item = PeerId> + '_
pub fn connected_peers(&self) -> impl Iterator<Item = PeerId> + '_
Returns an iterator of the ids of the connected peers.
Note: You have to call WebRtcSocket::update_peers
for this list to be accurate.
See also: WebRtcSocket::disconnected_peers
sourcepub fn disconnected_peers(&self) -> impl Iterator<Item = &PeerId>
pub fn disconnected_peers(&self) -> impl Iterator<Item = &PeerId>
Returns an iterator of the ids of peers that are no longer connected.
Note: You have to call WebRtcSocket::update_peers
for this list to be
accurate.
See also: WebRtcSocket::connected_peers
sourcepub fn id(&mut self) -> Option<PeerId>
pub fn id(&mut self) -> Option<PeerId>
Returns the id of this peer, this may be None
if an id has not yet
been assigned by the server.
sourcepub fn channel(&self, channel: usize) -> &WebRtcChannel
pub fn channel(&self, channel: usize) -> &WebRtcChannel
Gets an immutable reference to the WebRtcChannel
of a given id.
use matchbox_socket::*;
let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
.add_channel(ChannelConfig::reliable())
.add_channel(ChannelConfig::unreliable())
.build();
let is_closed = socket.channel(0).is_closed();
See also: WebRtcSocket::channel_mut
, WebRtcSocket::get_channel
,
WebRtcSocket::take_channel
§Panics
will panic if the channel cannot be found.
sourcepub fn channel_mut(&mut self, channel: usize) -> &mut WebRtcChannel
pub fn channel_mut(&mut self, channel: usize) -> &mut WebRtcChannel
Gets a mutable reference to the WebRtcChannel
of a given id.
use matchbox_socket::*;
let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
.add_channel(ChannelConfig::reliable())
.add_channel(ChannelConfig::unreliable())
.build();
let reliable_channel_messages = socket.channel_mut(0).receive();
See also: WebRtcSocket::channel
, WebRtcSocket::get_channel_mut
,
WebRtcSocket::take_channel
§Panics
will panic if the channel cannot be found.
sourcepub fn get_channel(
&self,
channel: usize
) -> Result<&WebRtcChannel, ChannelError>
pub fn get_channel( &self, channel: usize ) -> Result<&WebRtcChannel, ChannelError>
Gets an immutable reference to the WebRtcChannel
of a given id.
Returns an error if the channel was not found.
use matchbox_socket::*;
let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
.add_channel(ChannelConfig::reliable())
.add_channel(ChannelConfig::unreliable())
.build();
let is_closed = socket.get_channel(0).unwrap().is_closed();
See also: WebRtcSocket::get_channel_mut
, WebRtcSocket::take_channel
sourcepub fn get_channel_mut(
&mut self,
channel: usize
) -> Result<&mut WebRtcChannel, ChannelError>
pub fn get_channel_mut( &mut self, channel: usize ) -> Result<&mut WebRtcChannel, ChannelError>
Gets a mutable reference to the WebRtcChannel
of a given id.
Returns an error if the channel was not found.
use matchbox_socket::*;
let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
.add_channel(ChannelConfig::reliable())
.add_channel(ChannelConfig::unreliable())
.build();
let reliable_channel_messages = socket.get_channel_mut(0).unwrap().receive();
See also: WebRtcSocket::channel
, WebRtcSocket::take_channel
sourcepub fn take_channel(
&mut self,
channel: usize
) -> Result<WebRtcChannel, ChannelError>
pub fn take_channel( &mut self, channel: usize ) -> Result<WebRtcChannel, ChannelError>
Takes the WebRtcChannel
of a given id.
use matchbox_socket::*;
let (mut socket, message_loop) = WebRtcSocketBuilder::new("wss://example.invalid/")
.add_channel(ChannelConfig::reliable())
.add_channel(ChannelConfig::unreliable())
.build();
let reliable_channel = socket.take_channel(0).unwrap();
let unreliable_channel = socket.take_channel(1).unwrap();
See also: WebRtcSocket::channel
source§impl WebRtcSocket<SingleChannel>
impl WebRtcSocket<SingleChannel>
sourcepub fn receive(&mut self) -> Vec<(PeerId, Packet)>
pub fn receive(&mut self) -> Vec<(PeerId, Packet)>
Call this where you want to handle new received messages.
Messages are removed from the socket when called.
sourcepub fn try_send(
&mut self,
packet: Packet,
peer: PeerId
) -> Result<(), SendError>
pub fn try_send( &mut self, packet: Packet, peer: PeerId ) -> Result<(), SendError>
Try to send a packet to the given peer. An error is propagated if the socket future
is dropped. Ok
is not a guarantee of delivery.
source§impl WebRtcSocket<MultipleChannels>
impl WebRtcSocket<MultipleChannels>
sourcepub fn any_closed(&self) -> bool
pub fn any_closed(&self) -> bool
Returns whether any socket channel is closed
sourcepub fn all_closed(&self) -> bool
pub fn all_closed(&self) -> bool
Returns whether all socket channels are closed