GithubHelp home page GithubHelp logo

arrebagrove / p2p.net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phylliida/p2p.net

0.0 0.0 0.0 35.2 MB

Peer to peer networking in C# using WebRTC

License: MIT License

JavaScript 5.43% C# 94.57%

p2p.net's Introduction

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 :)

p2p.net's People

Contributors

phylliida 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.