-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async socket creation #136
Conversation
Looks like we just come across a new clippy false positive |
I still have an idea to get around the |
Transfering Edit: Just seen #140 |
73dce3e
to
6b5273c
Compare
Just curious, how could you use the socket in Bevy as a resource if you do this? I currently have something like this: #[derive(Resource, Default)]
struct SocketResource {
pub mb_socket: Option<WebRtcSocket>,
} Which I can then use to write Bevy events. I call these "SilkSocketEvents" (my internal abstraction library is called Silk). Here's an exact reference how I wrote that abstraction layer: fn event_writer(
mut socket_res: ResMut<SocketResource>,
mut event_wtr: EventWriter<SilkSocketEvent>,
mut connection_state: ResMut<State<ConnectionState>>,
) {
let socket_res = socket_res.as_mut();
if let Some(ref mut socket) = socket_res.mb_socket {
// Connection state updates
for (id, state) in socket.update_peers() {
match state {
matchbox_socket::PeerState::Connected => {
connection_state.set(ConnectionState::Connected).unwrap();
event_wtr.send(SilkSocketEvent::ConnectedToHost(id));
}
matchbox_socket::PeerState::Disconnected => {
connection_state
.set(ConnectionState::Disconnected)
.unwrap();
event_wtr.send(SilkSocketEvent::DisconnectedFromHost);
}
}
}
// Unreliable messages
event_wtr.send_batch(
socket
.receive_on_channel(SilkSocketConfig::UNRELIABLE_CHANNEL_INDEX)
.into_iter()
.map(SilkSocketEvent::Message),
);
// Reliable messages
event_wtr.send_batch(
socket
.receive_on_channel(SilkSocketConfig::RELIABLE_CHANNEL_INDEX)
.into_iter()
.map(SilkSocketEvent::Message),
);
// Id changed events
if let Some(id) = socket.id() {
if socket_res.id.is_none() {
socket_res.id.replace(id.clone());
event_wtr.send(SilkSocketEvent::IdAssigned(id));
}
}
}
} This is essentially how I translate my events for my games and projects. I'm not sure how I could that anymore. |
Apologies if I'm missing something, but I believe my changes to the bevy ggrs example demonstrate this with a channel used to return the socket to the bevy startup system and subsequently insert it as a resource |
I totally glanced over that. :) +1 |
Trying out creating the socket asynchronously, this should allow us to send the
PeerId
plus some arbitrary setup data before the socket is returned. This works really nicely if we're in an async context already, not as nice if we're in sync (e.g. bevy)