#networking #multiplayer-game #renet #gamedev #connection #bevy #management

bevy_renet

Bevy 插件用于 renet 库:支持认证和连接管理的服务器/客户端网络库,适用于多人游戏

12 个版本

0.0.12 2024年7月20日
0.0.11 2024年2月21日
0.0.10 2023年11月12日
0.0.9 2023年7月20日
0.0.4 2022年7月25日

520游戏开发 中排名

Download history 168/week @ 2024-05-04 195/week @ 2024-05-11 243/week @ 2024-05-18 230/week @ 2024-05-25 316/week @ 2024-06-01 200/week @ 2024-06-08 216/week @ 2024-06-15 233/week @ 2024-06-22 121/week @ 2024-06-29 181/week @ 2024-07-06 189/week @ 2024-07-13 408/week @ 2024-07-20 245/week @ 2024-07-27 209/week @ 2024-08-03 264/week @ 2024-08-10 242/week @ 2024-08-17

993 每月下载量
用于 6 个库(5 个直接使用)

MIT/Apache

160KB
2.5K SLoC

Bevy Renet

Latest version Documentation MIT Apache

Bevy 插件,用于 renet 库。一个用于服务器/客户端的网络库,具有密码学安全的认证和加密数据包。适用于快节奏的竞技型多人游戏。

用法

Bevy renet 是在 renet 库之上的一层小封装,它添加了系统以从客户端/服务器调用更新函数。需要将 RenetClientRenetServerNetcodeClientTransportNetcodeServerTransport 作为资源添加,因此设置与 renet 本身相似

服务器

let mut app = App::new();
app.add_plugin(RenetServerPlugin);

let server = RenetServer::new(ConnectionConfig::default());
app.insert_resource(server);

// Transport layer setup
app.add_plugin(NetcodeServerPlugin);
let server_addr = "127.0.0.1:5000".parse().unwrap();
let socket = UdpSocket::bind(server_addr).unwrap();
let server_config = ServerConfig {
    current_time: SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(),
    max_clients: 64, 
    protocol_id: 0,
    public_addresses: vec![server_addr], 
    authentication: ServerAuthentication::Unsecure
};
let transport = NetcodeServerTransport::new(server_config, socket).unwrap();
app.insert_resource(transport);

app.add_system(send_message_system);
app.add_system(receive_message_system);
app.add_system(handle_events_system);

// Systems

fn send_message_system(mut server: ResMut<RenetServer>) {
    let channel_id = 0;
    // Send a text message for all clients
    // The enum DefaultChannel describe the channels used by the default configuration
    server.broadcast_message(DefaultChannel::ReliableOrdered, "server message");
}

fn receive_message_system(mut server: ResMut<RenetServer>) {
    // Receive message from all clients
    for client_id in server.clients_id() {
        while let Some(message) = server.receive_message(client_id, DefaultChannel::ReliableOrdered) {
            // Handle received message
        }
    }
}

fn handle_events_system(mut server_events: EventReader<ServerEvent>) {
    for event in server_events.read() {
        match event {
            ServerEvent::ClientConnected { client_id } => {
                println!("Client {client_id} connected");
            }
            ServerEvent::ClientDisconnected { client_id, reason } => {
                println!("Client {client_id} disconnected: {reason}");
            }
        }
    }
}

客户端

let mut app = App::new();
app.add_plugin(RenetClientPlugin);

let client = RenetClient::new(ConnectionConfig::default());
app.insert_resource(client);

// Setup the transport layer
app.add_plugin(NetcodeClientPlugin);

let authentication = ClientAuthentication::Unsecure {
    server_addr: SERVER_ADDR,
    client_id: 0,
    user_data: None,
    protocol_id: 0,
};
let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
let mut transport = NetcodeClientTransport::new(current_time, authentication, socket).unwrap();

app.insert_resource(transport);

app.add_system(send_message_system);
app.add_system(receive_message_system);

// Systems

fn send_message_system(mut client: ResMut<RenetClient>) {
    // Send a text message to the server
    client.send_message(DefaultChannel::ReliableOrdered, "server message");
}

fn receive_message_system(mut client: ResMut<RenetClient>) {
    while let Some(message) = client.receive_message(DefaultChannel::ReliableOrdered) {
        // Handle received message
    }
}

示例

您可以使用以下命令运行 simple 示例:

  • 服务器:cargo run --features="serde transport" --example simple -- server
  • 客户端:cargo run --features="serde transport" --example simple -- client

如果您需要更复杂的示例,可以查看 demo_bevy 示例

Bevy Demo.webm

Bevy 兼容性

bevy bevy_renet
0.14 0.0.12
0.13 0.0.11
0.12 0.0.10
0.11 0.0.9
0.10 0.0.8
0.9 0.0.6
0.8 0.0.5
0.7 0.0.4

依赖项

~23MB
~423K SLoC