GithubHelp home page GithubHelp logo

wasm-peers / footballers Goto Github PK

View Code? Open in Web Editor NEW
14.0 14.0 1.0 296 KB

2D real-time mutliplayer game in a browser. Example usage of wasm-peers library.

License: Apache License 2.0

Rust 99.08% HTML 0.45% CSS 0.47%
game haxball rust webassembly webrtc

footballers's Introduction

wasm-peers

wasm-peers logo

crates.io docs.rs crates.io MIT/Apache 2.0 licensed CI coverage status pre-commit

This crate provides an easy-to-use wrapper around WebRTC and DataChannels for a peer-to-peer connections.

Overview

As creator of agar.io famously stated WebRTC is hard. This library aims to help, by abstracting away all the setup, and providing a simple way to send and receive messages over the data channel.

It's as easy as providing address to a signaling server instance from accompanying crate and specifying two callbacks. One that specifies what should happen when a connection is established, and one for when a message is received. After that you can send messages back and forth without worrying about the implementation details.

Library contains three network topologies, one-to-one, which creates an equal connection between two peers, one-to-many, which specifies a host and arbitrary number of clients and many-to-many that creates connection for each pair of peers and allows sending messages to any of them.

For a "production ready" apps built with this library check out either Live Document or Footballers.

Example

This example shows two peers sending ping and pong messages to each other.

use wasm_peers::ConnectionType;
use wasm_peers::one_to_one::NetworkManager;
use web_sys::console;

// there must be a signaling server from accompanying crate running on this port
const SIGNALING_SERVER_URL: &str = "ws://0.0.0.0:9001/one-to-one";

fn main() {
    // there must be some mechanism for exchanging session ids between peers
    let session_id = SessionId::new("some-session-id".to_string());
    let mut peer1 = NetworkManager::new(
        SIGNALING_SERVER_URL,
        session_id.clone(),
        &ConnectionType::Stun,
    ).unwrap();

    let peer1_clone = peer1.clone();
    let peer1_on_open = move || peer1_clone.send_message("ping!").unwrap();
    let peer1_on_message = {
        move |message| {
            console::log_1(&format!("peer1 received message: {}", message).into());
        }
    };
    peer1.start(peer1_on_open, peer1_on_message).unwrap();

    let mut peer2 = NetworkManager::new(
        SIGNALING_SERVER_URL,
        session_id,
        &ConnectionType::Stun,
    ).unwrap();
    let peer2_on_open = || { /* do nothing */ };
    let peer2_clone = peer2.clone();
    let peer2_on_message = {
        let peer2_received_message = peer2_received_message.clone();
        move |message| {
            console::log_1(&format!("peer2 received message: {}", message).into());
            peer2_clone.send_message("pong!").unwrap();
        }
    };
    peer2.start(peer2_on_open, peer2_on_message).unwrap();
}

For examples of other topologies check out the docs.

Roadmap

  • Add tests
  • Improve error handling
  • Introduce alternative API with futures
  • Add possibility to pass video/audio streams over established connection

Version History

  • 0.4
    • MiniServer callbacks don't accept UserId argument now (it'd always be hosts)
    • Fix documentation tests so that they compile
  • 0.3
    • Initial release to the public

Similar projects

License

This project is licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Authors

Tom Karwowski LinkedIn

Acknowledgments

These projects helped me grasp WebRTC in Rust:

Also, special thanks to the guys with whom I did my B.Eng. thesis.

footballers's People

Contributors

arkadiusz-gorecki avatar tomkarw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.