Crates.io | bevy_slinet |
lib.rs | bevy_slinet |
version | |
source | src |
created_at | 2022-02-15 23:51:50.54051 |
updated_at | 2024-11-30 11:08:00.328924 |
description | A simple networking plugin for bevy. |
homepage | https://github.com/aggyomfg/bevy_slinet#readme |
repository | https://github.com/aggyomfg/bevy_slinet |
max_upload_size | |
id | 532982 |
Cargo.toml error: | TOML parse error at line 21, column 1 | 21 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A simple networking plugin for bevy.
enum
), and receive events with deserialized packets.Note: Everything in bevy_slinet is feature-gated. Make sure to enable features you need (
client
,server
,protocol_tcp
,protocol_udp
,serializer_bincode
).
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use bevy_slinet::client::{ClientPlugin, ConnectionEstablishEvent, PacketReceiveEvent};
use bevy_slinet::packet_length_serializer::LittleEndian;
use bevy_slinet::protocols::tcp::TcpProtocol;
use bevy_slinet::serializers::bincode::{BincodeSerializer, DefaultOptions};
use bevy_slinet::ClientConfig;
struct Config;
impl ClientConfig for Config {
type ClientPacket = Packet;
type ServerPacket = Packet;
type Protocol = TcpProtocol;
type SerializerError = bincode::Error;
type LengthSerializer = LittleEndian<u32>;
fn build_serializer(
) -> SerializerAdapter<Self::ClientPacket, Self::ServerPacket, Self::SerializerError> {
SerializerAdapter::ReadOnly(Arc::new(
BincodeSerializer::<DefaultOptions>::default(),
))
}
}
#[derive(Serialize, Deserialize, Debug)]
enum ClientPacket {
String(String),
}
#[derive(Serialize, Deserialize, Debug)]
enum ServerPacket {
String(String),
}
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ClientPlugin::<Config>::connect("127.0.0.1:3000"))
.add_observer(connection_establish_system)
.add_observer(packet_receive_system)
.run()
}
fn connection_establish_system(event: Trigger<ConnectionEstablishEvent<Config>>) {
println!("Connected!");
}
fn packet_receive_system(event: Trigger<PacketReceiveEvent<Config>>) {
match &event.event().packet {
ServerPacket::String(s) => println!("Got a message: {}", s),
}
event
.event()
.connection
.send(ClientPacket::String("Hello, Server!".to_string()))
.unwrap();
}
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use bevy_slinet::packet_length_serializer::LittleEndian;
use bevy_slinet::protocols::tcp::TcpProtocol;
use bevy_slinet::serializers::bincode::{BincodeSerializer, DefaultOptions};
use bevy_slinet::server::{NewConnectionEvent, ServerPlugin, PacketReceiveEvent};
use bevy_slinet::ServerConfig;
struct Config;
impl ServerConfig for Config {
type ClientPacket = Packet;
type ServerPacket = Packet;
type Protocol = TcpProtocol;
type SerializerError = bincode::Error;
type LengthSerializer = LittleEndian<u32>;
fn build_serializer(
) -> SerializerAdapter<Self::ClientPacket, Self::ServerPacket, Self::SerializerError> {
SerializerAdapter::ReadOnly(Arc::new(
BincodeSerializer::<DefaultOptions>::default(),
))
}
}
#[derive(Serialize, Deserialize, Debug)]
enum ClientPacket {
String(String),
}
#[derive(Serialize, Deserialize, Debug)]
enum ServerPacket {
String(String),
}
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins(ServerPlugin::<Config>::bind("127.0.0.1:3000").unwrap())
.add_observer(new_connection_system)
.add_observer(packet_receive_system)
.run()
}
fn new_connection_system(event: Trigger<NewConnectionEvent<Config>>) {
event
.event()
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
}
fn packet_receive_system(event: Trigger<PacketReceiveEvent<Config>>) {
match &event.event().packet {
ClientPacket::String(s) => println!("Got a message from a client: {}", s),
}
event
.event()
.connection
.send(ServerPacket::String("Hello, Client!".to_string()))
.unwrap();
}
Note: you should implement keep-alive and disconnection systems yourself, or look at lobby_and_battle_servers example
Here.
Plugin Version | Bevy Version |
---|---|
0.1 |
0.6 |
0.2 |
0.6 |
0.3 |
0.7 |
0.4 |
0.8 |
0.5 |
0.9 |
0.6 |
0.10.1 |
0.7 |
0.11 |
0.8 |
0.12 |
0.9 |
0.13 |
0.10 |
0.13 |
0.11 |
0.14 |
0.12 |
0.14 |
0.13 |
0.15 |
main |
0.15 |