A cross platform (wasm included) networking library!

Overview

bootleg_networking

A cross platform (wasm included) networking library!

A networking plugin for the Bevy game engine that wraps around bevy_networking_turbulence and a custom TCP/UDP network server in order to make writing cross-platform multiplayer games fun and easy!

Getting started

Currently, the library is not published on crates.io, due to a few forks of other popular libraries it currently uses. Because of this, in order to use the libary, you must specify it as a git dependency

[dependencies]
bootleg_networking = { git = "https://github.com/billyb2/bootleg_networking" }

If you want to use this library with wasm, you should disable the native feature and enable the web feature

[dependencies]
bootleg_networking = { git = "https://github.com/billyb2/bootleg_networking", default-features = false, features = ["web"]}

I recommend pinning to a specific commit, since there are currently no stability guarantees.

Below is an example of how to use this library. It mainly shows how to setup a basic server, although a client is nearly identical

use std::sync::Arc;

use bevy::prelude::*;
use bevy::tasks::IoTaskPool;
use bootleg_networking::*;

const MESSAGE_CHANNEL_ID: MessageChannelID = MessageChannelID::new(0);
const MESSAGE_SETTINGS: MessageChannelSettings = MessageChannelSettings {
    channel: MESSAGE_CHANNEL_ID.id,
    channel_mode: MessageChannelMode::Unreliable,
    message_buffer_size: 256,
    packet_buffer_size: 256,
};

fn main() {
    let mut app = App::new();
    app
    .add_plugins(MinimalPlugins)
    .add_plugin(NetworkingPlugin)
    .add_startup_system(setup)
    .add_system(send)
    .add_system(receive);

    //Uncomment the line below!
    //app.run();
}

fn setup(mut commands: Commands, tokio_rt: Res<Runtime>, task_pool: Res<IoTaskPool>) {
    // First we need to actually initiate the NetworkReource. In this case, it's a server
    // We could use the new_client function if wanted a client
    let mut net = NetworkResource::new_server(tokio_rt.clone(), task_pool.0.clone());

    // Next, we need tell the server to setup listening
    // The equivalent function for clients is connect
    // Listen on ports 9000 for TCP and 9001 for UDP, and 9003
    // The first address is the one that the connect() function needs to use, and the other two are for WebRTC
    // Finally, the last argument is the maximum size of each packet. That argument is only necessary for native builds
    let listen_config = ListenConfig {
        tcp_addr: "127.0.0.1:9000",
        udp_addr: "127.0.0.1:9001",
        naia_addr: "127.0.0.1:9003",
        webrtc_listen_addr: "127.0.0.1:9004",
        public_webrtc_listen_addr: "127.0.0.1:9004",
    };

    net.listen(listen_config, Some(2048));
    // If we were calling net.connect, the first argument we would either have 9000 or 9003 as the port, depending on whether we were a native client or a web client
    // The second argument is only necessary on native builds, and it's asking for the UDP server SocketAddr
    /* let connect_config = ConnectConfig {
     *      addr: "127.0.0.1:9000",
     *      udp_addr: Some("127.0.0.1:9001"),
     * };
     *
     * net.connect(connect_config, Some(2048));
    */

    // We need to register for the native tcp/udp server and for naia seperately
    // Native registration
    net.register_message_channel_native(MESSAGE_SETTINGS, &MESSAGE_CHANNEL_ID).unwrap();
    // Naia registration
    net.set_channels_builder(|builder: &mut ConnectionChannelsBuilder| {
        builder
            .register::<String>(MESSAGE_SETTINGS)
            .unwrap();
    });

    // Finally, insert the network resource so it can be used by other systems
    commands.insert_resource(net);

}

// The following two functions are equivalent for both clients and servers, provided you've set up the NetworkResource properly

fn send(mut net: ResMut<NetworkResource>) {
    let message = String::from("Hello world");
    net.broadcast_message(&message, &MESSAGE_CHANNEL_ID).unwrap();

}

fn receive(mut net: ResMut<NetworkResource>) {
    let messages = net.view_messages::<String>(&MESSAGE_CHANNEL_ID).unwrap();

    for (_handle, message) in messages.iter() {
        println!("{}", message);

    }

}

Warning about sending messages larger than 1095 bytes

@totalkrill noticed that when attempting to send a message larger than 1095 bytes, the message wouldn't go through. This is due to a limitation in WebRTC that doesn't allow messages larger than 1200 bytes (which 1095 bytes + any overhead in the WebRTC protocol and anything Naia adds would definetly do). The NetworkResource will return an error upon trying to send a message above that size when sending it through Naia, though there isn't an issue on the TCP/UDP front. Just be warned though, that if you're trying to have cross platform compatibility, messages should be under that size.

If the crate isn't published on crates.io, how do I view the documentation?

Simply run cargo doc in the terminal. If you want to view the documentation for the web version of this crate, then run cargo doc --no-default-features --features web

Why make this crate

While working with the wonderful bevy_networking_turbulence library, I realized that I wasn't able to create network clients on anything other than wasm. While it is doable using the fantastic new WebRTC-rs library (and I even attempted it, it still currently isn't possible. After a lot of effort, I eventually gave up, and decided instead to write this library. While initially, it was just for internal use in a project I'm workin on, I realized that it would have a lot of potential as a public library.

You might also like...
Neutral cross-platform Rust game template

Rust Game Template Neutral cross-platform Rust game template. Build tool This project uses cargo-make task runner. It's required to build the project.

A lightweight, cross-platform epub reader.
A lightweight, cross-platform epub reader.

Pend Pend is a program for reading EPUB files. Check out the web demo! Preview Image(s) Installation Building Pend is simple & easy. You should be abl

Cross platform rendering in Rust
Cross platform rendering in Rust

Miniquad Miniquad is a manifestation of a dream in a world where we do not need a deep dependencies tree and thousands lines of code to draw things wi

A cross platform classic RPG game creator written in Rust.
A cross platform classic RPG game creator written in Rust.

Eldiron - Classic RPG Creation Create RPGs for every platform with Eldiron. Eldiron v1 will be able to create games similar to the classic Ultima seri

A safe, fast and cross-platform 2D component-based game framework written in rust

shura shura is a safe, fast and cross-platform 2D component-based game framework written in rust. shura helps you to manage big games with a component

Cross-platform GPU-accelerated viewer for the Mandelbrot set and similar (escape-time) fractals

fractal_viewer A cross-platform, GPU-accelerated viewer for the Mandelbrot Set and related fractals. Try it online! Usage Scroll wheel to zoom, click

Cross-platform compute shader engine
Cross-platform compute shader engine

wgpu-compute-toy This is the compute shader engine for https://compute.toys As well as running on the web via WebAssembly and WebGPU, it can run nativ

A cross-platform image (texture) viewer
A cross-platform image (texture) viewer

img_maniac A cross-platform image (texture) viewer Features Drag and drop images: Users can easily add as many images as they want to the main window

A networked (p2p), cross-platform physics simulation example using rollback netcode

bevy_gaff (work in progress) bevy_gaff is an attempt at making a networked (p2p), cross-platform physics simulation using rollback netcode. It synchro

Comments
  • Rooms

    Rooms

    Is there some kind of default way / batteries included way of implementing rooms? i.e. let clients connect to a room and only receive messages sent to that room. I suspect the channel ID might be useful for this?

    opened by janhohenheim 3
  • Implement clone for web runtime

    Implement clone for web runtime

    this allows for the pattern

    let net = NetworkResource::new_client(tokio_rt.clone(), task_pool.0.clone());
    

    for both native and web.

    Not sure about not having Option on the parameter though.

    opened by IsseW 1
  • Server stops working when a client disconnects

    Server stops working when a client disconnects

    When I connect with multiple clients to the server and I disconnect from one client, the server stops working. I also can't seem to receive the disconnect event when a client disconnects.

    I am on windows using Bevy 0.7.

    opened by tommygoris 0
  • Getting `Blocked loading mixed active content` on Firefox when hosting app over HTTPS.

    Getting `Blocked loading mixed active content` on Firefox when hosting app over HTTPS.

    I'm getting the Blocked loading mixed active content error when my WASM-Client tries to connect to the server. Is there a way to use SSL in the server implementation? If not, is there a way to trick Firefox into establishing the WebRTC connection anyways?

    opened by FF-AntiK 6
Owner
William Batista
University programmer who likes to make stuff
William Batista
Cross-platform (including wasm) persistent key value store plugin for rust games/apps

bevy_pkv bevy_pkv is a cross-platform persistent key value store for rust apps. Use it for storing things like settings, save games etc. Currently, it

Johan Klokkhammer Helsing 25 Jan 9, 2023
An attempt to create an easily customizable MMORPG game engine. Sky not included.

skyless Proof of concept of an easily customizable MMORPG game engine. Getting started Copy environment variables cp .env.example .env Start the engi

null 8 Nov 23, 2022
A simple authoritative server networking library for Bevy.

Bevy Networking Plugin This is a simple networking plugin for the Bevy game engine. This plugin provides the building blocks which game developers can

Matthew Fisher 35 Jan 1, 2023
General purpose client/server networking library written in Rust, built on top of the QUIC protocol which is implemented by quinn

Overview "This library stinks!" ... "Unless you like durian" durian is a client-server networking library built on top of the QUIC protocol which is i

Michael 92 Dec 31, 2022
Simple event-based client-server networking library for Bevy

Bevy Client Server Events Simple event-based client-server networking library for Bevy. Easily send bevy events to/from a client/server without worryi

Edouard Poitras 5 Sep 20, 2023
A Client/Server game networking plugin using QUIC, for the Bevy game engine.

Bevy Quinnet A Client/Server game networking plugin using QUIC, for the Bevy game engine. Bevy Quinnet QUIC as a game networking protocol Features Roa

Gilles Henaux 65 Feb 20, 2023
Tiny cross-platform webview library for C/C++/Golang. Uses WebKit (Gtk/Cocoa) and Edge (Windows)

webview A tiny cross-platform webview library for C/C++/Golang to build modern cross-platform GUIs. Also, there are Rust bindings, Python bindings, Ni

webview 10.8k Jan 9, 2023
Cross-platform game engine in Rust.

Cross-platform game engine in Rust.

Fedor Logachev 1.9k Jan 3, 2023
IDE for cross-platform software development

Diversity Space IDE for cross-platform software development | 日本語 | English | Русский | READMEの英語版とロシア語版はDeepl翻訳を使用して翻訳されています Английская и русская вер

latteS 0 Feb 23, 2022
This is a cross-platform tool to historicize different branches/depots/manifests and generating pseudocode for it to compare different game updates

CSHP This is a cross-platform tool to historicize different branches/depots/manifests and generating pseudocode for it to compare different game updat

raizo 6 Jan 28, 2022