Coder Social home page Coder Social logo

ramones156 / renet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lucaspoffo/renet

1.0 0.0 0.0 712 KB

Server/Client network library for multiplayer games with authentication and connection management made with Rust

License: Apache License 2.0

Rust 100.00%

renet's Introduction

Renet

Latest version Documentation MIT Apache

Renet is a network library for Server/Client games written in rust. Built on top of UDP, it is focused on fast-paced games such as FPS, and competitive games that need authentication. Provides the following features:

  • Client/Server connection management
  • Authentication and encryption, checkout renetcode
  • Multiple types of channels:
    • Reliable Ordered: garantee ordering and delivery of all messages
    • Unreliable Unordered: messages that don't require any garantee of delivery or ordering
    • Block Reliable: for bigger messages, such as level initialization
  • Packet fragmention and reassembly

Sections:

Usage

Renet aims to have a simple API that is easy to integrate with any code base. Pool for new messages at the start of a frame with update, messages sent during a frame - or that need to be resent - are aggregated and sent together with sent_packets.

Server

let delta_time = Duration::from_millis(16);
let mut server = RenetServer::new(...);
let channel_id = 0;

// Your gameplay loop
loop {
    // Receive new messages and update clients
    server.update(delta_time)?;
    
    // Check for client connections/disconnections
    while let Some(event) = server.get_event() {
        match event {
            ServerEvent::ClientConnected(id, user_data) => {
                println!("Client {} connected", id);
            }
            ServerEvent::ClientDisconnected(id) => {
                println!("Client {} disconnected", id);
            }
        }
    }

    // Receive message from channel
    for client_id in server.clients_id().into_iter() {
        while let Some(message) = server.receive_message(client_id, channel_id) {
            // Handle received message
        }
    }
    
    // Send a text message for all clients
    server.broadcast_message(channel_id, "server message".as_bytes().to_vec());
    
    // Send message to only one client
    let client_id = ...;
    server.send_message(client_id, channel_id, "server message".as_bytes().to_vec());
 
    // Send packets to clients
    server.send_packets()?;
}

Client

let delta_time = Duration::from_millis(16);
let mut client = RenetClient::new(...);
let channel_id = 0;

// Your gameplay loop
loop {
    // Receive new messages and update client
    client.update(delta_time)?;
    
    if client.is_connected() {
        // Receive message from server
        while let Some(message) = client.receive_message(channel_id) {
            // Handle received message
        }
        
        // Send message
        client.send_message(channel_id, "client text".as_bytes().to_vec());
    }
 
    // Send packets to server
    client.send_packets()?;
}

Demos

You can checkout the echo example for a simple usage of the library. Or you can look into the two demos that have more complex uses of renet:

Bevy Demo
Simple bevy application to demonstrate how you could replicate entities and send reliable messages as commands from the server/client using renet:

Bevy.Demo.webm

Repository

Chat Demo
Simple chat application made with egui to demonstrate how you could handle errors, states transitions and client self hosting:

Chat.Demo.webm

Repository

Plugins

Checkout bevy_renet if you want to use renet as a plugin with the Bevy engine.

Visualizer

Checkout renet_visualizer for a egui plugin to plot metrics data from renet clients and servers:

renet_visualizer.mp4

renet's People

Contributors

aceeri avatar dependabot[bot] avatar lucaspoffo avatar shatur avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.