Crates.io | bevy_octopus |
lib.rs | bevy_octopus |
version | 0.3.0 |
source | src |
created_at | 2024-07-08 05:47:02.857985 |
updated_at | 2024-07-20 14:16:13.37606 |
description | ECS based networking library for Bevy |
homepage | https://github.com/foxzool/bevy_octopus |
repository | https://github.com/foxzool/bevy_octopus |
max_upload_size | |
id | 1295391 |
size | 205,708 |
A Low-level ECS-driven network plugin for Bevy.
Add this in your Cargo.toml:
[dependencies]
bevy_octopus = { version = "0.3", "features" = ["serde_json", "bincode"]} # or your custom format
use bevy::prelude::*;
use bevy_octopus::{
prelude::*,
transports::{tcp::TcpAddress, udp::UdpAddress},
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PlayerInformation {
pub health: usize,
pub position: (u32, u32, u32),
}
const TCP_CHANNEL: ChannelId = ChannelId("tcp");
const UDP_CHANNEL: ChannelId = ChannelId("udp");
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(OctopusPlugin)
// UDP CHANNEL use json tranformer for PlayerInformation struct
.add_transformer::<PlayerInformation, JsonTransformer>(UDP_CHANNEL)
// TCP_CHANNEL use json tranformer for PlayerInformation struct
.add_transformer::<PlayerInformation, BincodeTransformer>(TCP_CHANNEL)
.add_systems(Startup, setup)
.add_systems(Update, resend_udp_to_tcp)
.observe(on_node_event)
.run();
}
fn setup(mut commands: Commands) {
// tcp client
commands.spawn((
NetworkBundle::new(TCP_CHANNEL),
ClientNode(TcpAddress::new("127.0.0.1:4321")),
));
// udp server
commands.spawn((
NetworkBundle::new(UDP_CHANNEL),
ServerNode(UdpAddress::new("127.0.0.1:4002")),
));
}
pub fn on_node_event(trigger: Trigger<NetworkEvent>) {
info!("{:?} trigger {:?}", trigger.entity(), trigger.event());
}
pub fn resend_udp_to_tcp(
mut channel_recviced: EventReader<ReceiveChannelMessage<PlayerInformation>>,
mut ev_send: EventWriter<SendChannelMessage<PlayerInformation>>,
) {
for event in channel_recviced.read() {
info!("recevice {:?}", event.message);
if event.channel_id == UDP_CHANNEL {
ev_send.send(SendChannelMessage {
channel_id: TCP_CHANNEL,
message: event.message.clone(),
});
}
}
}
Every network node is a component, so you can easily manage network entities with Bevy ECS.
Apps can be many servers and many clients at the same time.
You can define channel transformers for data serialization and deserialization.
Support UDP unicast, broadcast, multicast. example
Protocol | Server | Client | Sever with SSL | Client with SSL |
---|---|---|---|---|
UDP | ✅ | ✅ | ✘ | ✘ |
TCP | ✅ | ✅ | ☐ | ☐ |
Websocket | ✅ | ✅ | ☐ | ☐ |
ServerNode | ClientNode | NetworkPeer | |
---|---|---|---|
server | ✓ | ||
client | ✓ | ||
client session | ✓ | ✓ |
bevy | bevy_octopus |
---|---|
0.14 | 0.2 |
0.13 | 0.1 |
All code in this repository is dual-licensed under either:
at your option. This means you can select the license you prefer.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.