bevy_slinet

A simple networking plugin for bevy.

docs.rs Crates.io Crates.io

Features

Client example

```rust use bevy::app::{App, EventReader}; use bevy::MinimalPlugins; use serde::{Deserialize, Serialize};

use bevyslinet::client::{ClientPlugin, ConnectionEstablishEvent, PacketReceiveEvent}; use bevyslinet::packetlengthserializer::LittleEndian; use bevyslinet::protocols::tcp::TcpProtocol; use bevyslinet::serializers::bincode::{BincodeSerializer, DefaultOptions}; use bevy_slinet::ClientConfig;

struct Config;

impl ClientConfig for Config { type ClientPacket = ClientPacket; type ServerPacket = ServerPacket; type Protocol = TcpProtocol; type Serializer = BincodeSerializer; type LengthSerializer = LittleEndian; }

[derive(Serialize, Deserialize, Debug)]

enum ClientPacket { String(String), }

[derive(Serialize, Deserialize, Debug)]

enum ServerPacket { String(String), }

fn main() { App::new() .addplugins(MinimalPlugins) .addplugin(ClientPlugin::::connect("127.0.0.1:3000")) .addsystem(connectionestablishsystem) .addsystem(packetreceivesystem) .run() }

fn connectionestablishsystem(mut events: EventReader>) { for _event in events.iter() { println!("Connected!"); } }

fn packetreceivesystem(mut events: EventReader>) { for event in events.iter() { match &event.packet { ServerPacket::String(s) => println!("Got a message: {}", s), } event .connection .send(ClientPacket::String("Hello, Server!".to_string())).unwrap(); } } ```

Server Example

```rust use bevy::app::{App, EventReader}; use bevy::MinimalPlugins; use serde::{Deserialize, Serialize};

use bevyslinet::packetlengthserializer::LittleEndian; use bevyslinet::protocols::tcp::TcpProtocol; use bevyslinet::serializers::bincode::{BincodeSerializer, DefaultOptions}; use bevyslinet::server::{NewConnectionEvent, ServerPlugin, PacketReceiveEvent}; use bevy_slinet::ServerConfig;

struct Config;

impl ServerConfig for Config { type ClientPacket = ClientPacket; type ServerPacket = ServerPacket; type Protocol = TcpProtocol; type Serializer = BincodeSerializer; type LengthSerializer = LittleEndian; }

[derive(Serialize, Deserialize, Debug)]

enum ClientPacket { String(String), }

[derive(Serialize, Deserialize, Debug)]

enum ServerPacket { String(String), }

fn main() { App::new() .addplugins(MinimalPlugins) .addplugin(ServerPlugin::::bind("127.0.0.1:3000").unwrap()) .addsystem(newconnectionsystem) .addsystem(packetreceivesystem) .run() }

fn newconnectionsystem(mut events: EventReader>) { for event in events.iter() { event .connection .send(ServerPacket::String("Hello, Client!".to_string())).unwrap(); } }

fn packetreceivesystem(mut events: EventReader>) { for event in events.iter() { match &event.packet { ClientPacket::String(s) => println!("Got a message from a client: {}", s), } event .connection .send(ServerPacket::String("Hello, Client!".to_string())).unwrap(); } } ```

Note: you should implement keep-alive and disconnection systems yourself, or look at lobbyandbattle_servers example

More examples

Here.

Compatibility table

| Plugin Version | Bevy Version | |----------------|--------------| | 0.1 | 0.6 | | 0.2 | 0.6 | | main | main |