move things around
This commit is contained in:
commit
1c5b22c93c
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
lib-cov
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.gz
|
||||
|
||||
pids
|
||||
logs
|
||||
results
|
||||
|
||||
node_modules
|
||||
npm-debug.log
|
92
lib/server.js
Normal file
92
lib/server.js
Normal file
@ -0,0 +1,92 @@
|
||||
var WebSocketServer = require('ws').Server;
|
||||
var util = require('./util');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
|
||||
function PeerServer(options) {
|
||||
if (!(this instanceof PeerServer)) return new PeerServer(options);
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
|
||||
options = util.extend({
|
||||
port: 80
|
||||
}, options);
|
||||
|
||||
var wss = new WebSocketServer({ port: options.port });
|
||||
|
||||
|
||||
this.clients = {};
|
||||
var self = this;
|
||||
|
||||
// For connecting clients:
|
||||
// Src will connect upon creating a link.
|
||||
// Receivers will connect after clicking a button and entering an optional key.
|
||||
wss.on('connection', function(socket) {
|
||||
var clientId = util.randomId();
|
||||
while (!!self.clients[clientId]) {
|
||||
clientId = util.randomId();
|
||||
}
|
||||
self.clients[clientId] = socket;
|
||||
|
||||
socket.on('message', function(data) {
|
||||
var message = JSON.parse(data);
|
||||
if (options.debug) {
|
||||
console.log('PeerServer: ', message);
|
||||
}
|
||||
|
||||
switch (message.type) {
|
||||
// Source connected -- send back its ID.
|
||||
case 'SOURCE':
|
||||
socket.send(JSON.stringify({ type: 'SOURCE-ID', id: clientId }));
|
||||
break;
|
||||
// Sink connected -- send back its ID and notify src.
|
||||
case 'SINK':
|
||||
if (!!message.source && !!self.clients[message.source]) {
|
||||
self.clients[message.source].send(JSON.stringify({
|
||||
type: 'SINK-CONNECTED', sink: clientId }));
|
||||
|
||||
socket.send(JSON.stringify({ type: 'SINK-ID', id: clientId }));
|
||||
} else {
|
||||
util.prettyError('source invalid');
|
||||
}
|
||||
break;
|
||||
case 'LEAVE':
|
||||
if (!!self.clients[message.dst]) {
|
||||
try {
|
||||
self.clients[message.dst].send(data);
|
||||
} catch (e) {
|
||||
if (options.debug) {
|
||||
console.log('Error', e);
|
||||
}
|
||||
}
|
||||
delete self.clients[message.src];
|
||||
}
|
||||
break;
|
||||
// Offer or answer from src to sink.
|
||||
case 'OFFER':
|
||||
case 'ANSWER':
|
||||
case 'CANDIDATE':
|
||||
case 'PORT':
|
||||
if (!!self.clients[message.dst]) {
|
||||
try {
|
||||
self.clients[message.dst].send(data);
|
||||
} catch (e) {
|
||||
if (options.debug) {
|
||||
console.log('Error', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
util.prettyError('message unrecognized');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
util.inherits(PeerServer, EventEmitter);
|
||||
|
||||
exports.PeerServer = PeerServer;
|
32
lib/util.js
Normal file
32
lib/util.js
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
var util = {
|
||||
inherits: function(ctor, superCtor) {
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = Object.create(superCtor.prototype, {
|
||||
constructor: {
|
||||
value: ctor,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
},
|
||||
extend: function(dest, source) {
|
||||
for(var key in source) {
|
||||
if(source.hasOwnProperty(key)) {
|
||||
dest[key] = source[key];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
},
|
||||
randomId: function () {
|
||||
return Math.random().toString(36).substr(2);
|
||||
},
|
||||
prettyError: function (msg) {
|
||||
console.log('PeerServer: ', msg);
|
||||
}
|
||||
};
|
||||
|
||||
// if node
|
||||
module.exports = util;
|
||||
// end node
|
16
package.json
Normal file
16
package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "peer",
|
||||
"version": "0.0.1",
|
||||
"description": "Simple p2p file transfer",
|
||||
"main": "lib/server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js"
|
||||
},
|
||||
"repository": "",
|
||||
"author": "Michelle Bu",
|
||||
"license": "BSD",
|
||||
"dependencies": {
|
||||
"ws": "~0.4.25"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user