Skip to content

Commit 5ed81dc

Browse files
author
Matthew Fisher
committed
broadcast event example
Signed-off-by: Matthew Fisher <[email protected]>
1 parent a8284ce commit 5ed81dc

File tree

5 files changed

+118
-12
lines changed

5 files changed

+118
-12
lines changed

examples/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,43 @@ In one terminal, run:
1010

1111
```
1212
cd bevy_simple_networking
13-
cargo run --example server
13+
cargo run --example simple_server
1414
```
1515

1616
In another terminal, run:
1717

1818
```
1919
cd bevy_simple_networking
20-
cargo run --example client
20+
cargo run --example simple_client
2121
```
2222

2323
You should see a message that the client connected in the server logs:
2424

2525
```
26-
$ cargo run --example server
26+
$ cargo run --example simple_server
2727
Compiling bevy_simple_networking v0.1.2 (/home/bacongobbler/code/bevy_simple_networking)
2828
Finished dev [unoptimized + debuginfo] target(s) in 8.54s
29-
Running `target/debug/examples/server`
30-
Oct 12 15:17:01.024 INFO server: 127.0.0.1:37810: connected!
29+
Running `target/debug/examples/simple_server`
30+
Oct 12 15:17:01.024 INFO simple_server: 127.0.0.1:37810: connected!
3131
```
3232

3333
And you should see a response from the server in the client logs:
3434

3535
```
36-
$ cargo run --example client
36+
$ cargo run --example simple_client
3737
Finished dev [unoptimized + debuginfo] target(s) in 0.09s
38-
Running `target/debug/examples/client`
39-
Oct 12 16:16:17.097 INFO client: server sent a message: b"PONG"
38+
Running `target/debug/examples/simple_client`
39+
Oct 12 16:16:17.097 INFO simple_client: server sent a message: b"PONG"
4040
```
4141

4242
Now close the client by pressing CTRL+C (CMD+C on MacOS). After a few seconds,
4343
you should see the following in the server logs:
4444

4545
```
46-
$ cargo run --example server
46+
$ cargo run --example simple_server
4747
Compiling bevy_simple_networking v0.1.2 (/home/bacongobbler/code/bevy_simple_networking)
4848
Finished dev [unoptimized + debuginfo] target(s) in 8.54s
49-
Running `target/debug/examples/server`
50-
Oct 12 15:17:01.024 INFO server: 127.0.0.1:37810: connected!
51-
Oct 12 15:17:07.024 INFO server: 127.0.0.1:37810: disconnected!
49+
Running `target/debug/examples/simple_server`
50+
Oct 12 15:17:01.024 INFO simple_server: 127.0.0.1:37810: connected!
51+
Oct 12 15:17:07.024 INFO simple_server: 127.0.0.1:37810: disconnected!
5252
```

examples/chat_client.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::net::{SocketAddr, UdpSocket};
2+
3+
use bevy::{log::LogPlugin, prelude::*};
4+
use bevy_simple_networking::{ClientPlugin, NetworkEvent};
5+
6+
fn main() {
7+
let remote_addr: SocketAddr = "127.0.0.1:4567".parse().expect("could not parse addr");
8+
let socket = UdpSocket::bind("[::]:0").expect("could not bind socket");
9+
socket
10+
.connect(remote_addr)
11+
.expect("could not connect to server");
12+
socket
13+
.set_nonblocking(true)
14+
.expect("could not set socket to be nonblocking");
15+
16+
App::build()
17+
.insert_resource(remote_addr)
18+
.insert_resource(socket)
19+
.add_plugins(MinimalPlugins)
20+
.add_plugin(LogPlugin)
21+
.add_plugin(ClientPlugin)
22+
.add_system(connection_handler.system())
23+
.run();
24+
}
25+
26+
fn connection_handler(mut events: EventReader<NetworkEvent>) {
27+
for event in events.iter() {
28+
match event {
29+
NetworkEvent::Message(_, msg) => {
30+
info!("{:?}", msg);
31+
}
32+
NetworkEvent::SendError(err, msg) => {
33+
error!(
34+
"NetworkEvent::SendError (payload [{:?}]): {:?}",
35+
msg.payload, err
36+
);
37+
}
38+
NetworkEvent::RecvError(err) => {
39+
error!("NetworkEvent::RecvError: {:?}", err);
40+
}
41+
// discard irrelevant events
42+
_ => {}
43+
}
44+
}
45+
}

examples/chat_server.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::{net::UdpSocket, time::Duration};
2+
3+
use bevy::{app::ScheduleRunnerSettings, log::LogPlugin, prelude::*};
4+
use bevy_simple_networking::{NetworkEvent, NetworkResource, ServerPlugin, Transport};
5+
6+
const LISTEN_ADDRESS: &str = "127.0.0.1:4567";
7+
8+
fn main() {
9+
let socket = UdpSocket::bind(LISTEN_ADDRESS).expect("could not bind socket");
10+
socket
11+
.set_nonblocking(true)
12+
.expect("could not set socket to be nonblocking");
13+
socket
14+
.set_read_timeout(Some(Duration::from_secs(5)))
15+
.expect("could not set read timeout");
16+
17+
info!("Server now listening on {}", LISTEN_ADDRESS);
18+
19+
App::build()
20+
// run the server at a reduced tick rate (100 ticks per minute)
21+
.insert_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f32(
22+
60. / 100.,
23+
)))
24+
.insert_resource(socket)
25+
.add_plugins(MinimalPlugins)
26+
.add_plugin(LogPlugin)
27+
.add_plugin(ServerPlugin)
28+
.add_system(connection_handler.system())
29+
.run();
30+
}
31+
32+
fn connection_handler(
33+
net: Res<NetworkResource>,
34+
mut events: EventReader<NetworkEvent>,
35+
mut transport: ResMut<Transport>,
36+
) {
37+
for event in events.iter() {
38+
match event {
39+
NetworkEvent::Connected(handle) => {
40+
for (addr, _) in net.connections.iter() {
41+
transport.send(*addr, format!("{} has entered the chat", handle).as_bytes());
42+
}
43+
}
44+
NetworkEvent::Disconnected(handle) => {
45+
for (addr, _) in net.connections.iter() {
46+
transport.send(*addr, format!("{} has left the chat", handle).as_bytes());
47+
}
48+
}
49+
NetworkEvent::SendError(err, msg) => {
50+
info!(
51+
"NetworkEvent::SendError (payload [{:?}]): {:?}",
52+
msg.payload, err
53+
);
54+
}
55+
NetworkEvent::RecvError(err) => {
56+
info!("NetworkEvent::RecvError: {:?}", err);
57+
}
58+
_ => {}
59+
}
60+
}
61+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)