udp2p
v0.2.17
Published
P2P connection with UDP hole punching
Downloads
30
Readme
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);});