• Stars
    star
    169
  • Rank 223,432 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created about 7 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Peer to peer networking in C# using WebRTC

P2P.NET

Peer to peer networking in C# using WebRTC

Update: if you are using Unity, I have two packages uploaded in the unitypackage folder. I recommend using UnityWebsocketP2P.unitypackage, it doesn't use WebRTC and instead only uses websockets. It should work more reliably and actually calls the OnDisconnect callback, among other things.

This technically works, however it has some issues with latency with lots of clients, and the disconnect callback is never called. I've been working on it but am not to a stable state yet because chromium's webrtc api is awful and has lots of issues that I keep running into. Honestly I would recommend using the WebRTC Network plugin if you are using Unity, as it does everything my software does and more for free. The author also has A video and audio chat application for $95 if you need that as well. I am in no way associated with that author, but his tools are stictly better than mine so I'd recommend using them until mine is in a better place.

This is designed to be pretty easy to use, here is an example:

class SampleUsage
{
    Peer myPeer;
    public SampleUsage() {
        // See below for how to make your own signaling server if you want
        myPeer = new Peer("ws://sample-bean.herokuapp.com", "anystring this is your room id");
        myPeer.OnBytesFromPeer += Peer_OnBytesFromPeer;
        myPeer.OnConnection += Peer_OnConnection;
        myPeer.OnDisconnection += Peer_OnDisconnection;
        myPeer.OnGetID += Peer_OnGetID;
        myPeer.OnTextFromPeer += Peer_OnTextFromPeer;
    }

    void Peer_OnGetID(string id)
    {
        Console.WriteLine("my id is " + id);
    }

    void Peer_OnConnection(string peer)
    {
        Console.WriteLine(peer + " connected");
        myPeer.Send(peer, "hi there");
        myPeer.Send(peer, new byte[] {1, 27, 28});
    }

    void Peer_OnDisconnection(string peer)
    {
        Console.WriteLine(peer + " disconnected");
    }

    void Peer_OnTextFromPeer(string peer, string text)
    {
        Console.WriteLine(peer + " sent " + text);
    }

    void Peer_OnBytesFromPeer(string peer, byte[] bytes)
    {
        Console.WriteLine(peer + " sent " + bytes.Length + " bytes");
    }

    // Call peer.Dispose() when you are done using it
    void OnClose()
    {
        myPeer.Dispose(); // it is fine to call this more then once
    }
    
    // Call peer.Update() often (every frame is fine). It will call your callbacks on the thread you call Update()
    void Tick ()
    {
        myPeer.Update();
    }
}

All the work is done on a seperate thread (that is then passed to thread-safe queues that are dequeued when you call Update()) so it is all non-blocking.

You can simply use my signaling server for testing. If you want to host your own (for free), you can do the following:

  1. Make an account on https://www.heroku.com/. It is free and doesn't even require you to enter payment info
  2. Go into https://dashboard.heroku.com and click New, then Create New App.
  3. Pick some random app name or let it choose one for you it doesn't really matter because this is just the websocket url that no one will see. Mine is nameless-scrubland-88927 but insert yours into the commands below instead
  4. Install the heroku cli, nodejs, and npm
  5. Clone this repo

git clone https://github.com/Phylliida/P2P.NET.git

then copy the server code (signalingServer) into a directory without the .git file

  1. In that directory, to login call

heroku login

  1. Then create a new git repo there that will store your server code (Heroku works by having a git repo you manage your server code with)

git init

heroku git:remote -a nameless-scrubland-88927

(except use your server instead)

  1. Now deploy your server

npm install

git add .

git commit -am "Initial upload"

git push -f heroku master

The -f (force) you shouldn't usually do but we need to override the initial configuration and this is the easiest way to do that.

You can tweak the config.json file if you want but that isn't really needed. You should also generate your own certificates in production but these work fine for testing small projects.

Now in the code above you can replace

myPeer = new Peer("ws://sample-bean.herokuapp.com", "anystring this is your room id");

with

myPeer = new Peer("ws://nameless-scrubland-88927.herokuapp.com", "anystring this is your room id");

except use your server instead :)

More Repositories

1

UnityWindowsCapture

A framework to capture individual windows or the entire desktop in Unity
C#
132
star
2

orbslam-windows

Easy build for ORB Slam 2 on Windows
C++
130
star
3

Dialogue-Datasets

A collection of plain text dialogue datasets
83
star
4

UnityVOIP

[Doesn't work now sorry] Real time peer to peer voice chat in Unity, using CSCore, NSpeex, and UnityP2P
C#
49
star
5

UnityP2P

Peer to peer networking in Unity using WebRTC and a free Heroku server for signaling. This repo has old broken code so I removed it, use https://github.com/Phylliida/P2P.NET now
42
star
6

UnityMatrix

A Matrix class for Unity C# that is much better than the Matrix4x4 class
C#
14
star
7

PyTorchWindowsBinary

Precompiled binaries for PyTorch with CUDA for Python 3.5 and 3.6 on Windows x64
12
star
8

slam-libs

Lots of windows binaries needed for building orb-slam, lsd-slam, etc. Built in x64 Release Mode
C++
11
star
9

SimpleUnityPhysics

A custom physics engine for Unity3D with joints that actually work. Designed for a genetic algorithm so, for example, 10 seconds of simulation can run in a few milliseconds on an average computer.
C#
11
star
10

MambaLens

Mamba support for transformer lens
Python
10
star
11

P2PDraw

Tiltbrush/Quill 3D drawing program that is open source and peer to peer
ASP
7
star
12

openmuscle

An open source implementation of the Hill-type muscle model without any dependencies.
C#
7
star
13

Blocks

Tinkering with making an open source minecraft clone in unity
C#
6
star
14

suitesparse-windows-binaries

Prebuilt binaries of SuiteSparse for Windows because they are gross to build
C
4
star
15

UnityP2P-Broken

UDP hole punching in Unity3D in C# for peer to peer networking, using PubNub as a free signaling server. This doesn't actually work probably don't use this
C#
4
star
16

mamba_interp

Various stuff in progress for mamba interpretability
Jupyter Notebook
4
star
17

WorldSeed

A framework for easy creation of living creatures, plants, and terrain.
C#
3
star
18

UnityWASAPI

An example project that allows your program to interact with any sound a user plays to their speakers
C#
3
star
19

twitterconversations

Python
3
star
20

charRNN

Recurrent neural networks written in tensorflow that are fast, easy to use, and can reproduce "The Unreasonable Effectiveness of Recurrent Neural Networks"
Python
3
star
21

GeoLib

A simple javascript library for making games like pokemon go
JavaScript
2
star
22

SoundMirroring

Unity C# Code that lets you mirror any audio device to any other audio device
C#
2
star
23

ttswrapper

neos tts local webserver
Python
1
star
24

mtgspritesheet

Makes a Sprite Sheet out of a MTG deck using Scryfall
Python
1
star
25

StableDiffusionLargeResPatch

Patch for stable diffusion that let you run it at large resolutions (up to 2992x2992 on 8GB VRAM)
Python
1
star
26

GeoCreatures

AR game where you gather items for raising creatures by walking around in the real world
1
star
27

UnityMirrorBox

A mirror box VR Application using the Oculus Touch for helping regain movement in your hands
C#
1
star
28

ResoniteEasyFunctionWrapperExampleMod

Example usage of ResoniteEasyFunctionWrapper
C#
1
star