Git Product home page Git Product logo

udp2p's Introduction

UDP2P

P2P connection with UDP hole punching

Install

npm install udp2p

Quick start

var udp2p = require('udp2p');
var client = new udp2p();

client.connect(function () {
  client.fetchClient(function(e, d) {
    d.map(function(v) {
      var c = v.name;
      client.peerMsg({message: 'Hello, ' + c}, c, function (ee, dd) {});
    })
  });
});

Provides hole punching services (Server Mode)

var udp2p = require('udp2p');
var tracker = {
  address: 'tracker.cc-wei.com',
  port: 2266
};
var options = {
  server: true,
  port: 2266,
  tracker: [tracker]
};

var server = new udp2p(options);

Connect with hole punching server (Client Mode)

var udp2p = require('udp2p');
var client = new udp2p();
var server = {
  address: 'tracker.cc-wei.com',
  port: 2266
};

client.connect(server, function(err) {
  console.log("status:", client.getStatus());
});

Fetch client list

client.fetchClient(function(err, list) {
  console.log(list);
});

Send message

var message = {
  message: 'Hello UDP2P!'
};

var peer = client.getClientList().pop().name;
client.peerMsg(message, peer, function(err, response) {});

Send file

var filepath = '/Users/luphia/Documents/Workspace/Playground/logo.png';
var peer = client.getClientList().pop().name;
client.peerFile(filepath, peer, function(err, response) {});

Setup event

var savePath = '/Users/luphia/Desktop/';

client.on('message', function (data) {
  console.log('%s: %s', data.from, JSON.stringify(data.content));
});

client.on('file', function (data) {
  console.log('recieve file [%s] from %s', data.from, data.name);
  data.r2x.save(savePath + data.name);
});

Setup Reaaction

client.on('message', function (d) {
  var file = d.content.file;
  if(file) {
    console.log(file);
    client.response(file, d);
  }
  else {
    console.log(d);
  }
});

Request a file

var filepath = '/Users/luphia/Desktop/upload/crownfond.png';
client.request({file: filepath}, 'client2', function (d) {console.log('Response'); console.log(d);});

udp2p's People

Contributors

luphia avatar

Watchers

James Cloos 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.