10个重大版本发布

0.11.0 2024年7月21日
0.9.0 2024年2月29日
0.7.0 2023年9月17日
0.6.0 2023年4月17日
0.2.1 2022年2月21日

#117游戏开发

Download history 17/week @ 2024-04-27 1/week @ 2024-05-04 3/week @ 2024-06-08 1/week @ 2024-06-15 122/week @ 2024-07-20 10/week @ 2024-07-27

132 每月下载量

Apache-2.0 OR MIT

100KB
2K SLoC

bevy_slinet

bevy的一个简单网络插件。

docs.rs Crates.io Crates.io

特性

  • 您可以选择TCP或UDP协议。添加自己的协议就像实现几个特质的那么简单。
  • 具有不同配置的多个客户端/服务器(指定协议、数据包类型、序列化器等)
  • 反序列化。您选择一个序列化格式、数据包类型(您可能希望它是 enum),并接收反序列化数据包的事件。

注意:bevy_slinet中的所有内容都受功能门控。请确保启用您需要的功能(clientserverprotocol_tcpprotocol_udpserializer_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_systems(Update, (connection_establish_system, packet_receive_system))
        .run()
}

fn connection_establish_system(mut events: EventReader<ConnectionEstablishEvent<Config>>) {
    for _event in events.read() {
        println!("Connected!");
    }
}

fn packet_receive_system(mut events: EventReader<PacketReceiveEvent<Config>>) {
    for event in events.read() {
        match &event.packet {
            ServerPacket::String(s) => println!("Got a message: {}", s),
        }
        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_systems(Update, (new_connection_system, packet_receive_system))
        .run()
}

fn new_connection_system(mut events: EventReader<NewConnectionEvent<Config>>) {
    for event in events.read() {
        event
            .connection
            .send(ServerPacket::String("Hello, Client!".to_string())).unwrap();
    }
}

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

注意:您应该自己实现保持活动状态和断开连接系统,或者查看 lobbies_and_battle_servers 示例

更多示例

在此.

兼容性表

插件版本 Bevy版本
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.14

依赖项

~22–34MB
~531K SLoC