forked from whytheplatypus/peerjs-server
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1c5b22c
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |