|
@@ -5,6 +5,12 @@ var EventEmitter = require('events').EventEmitter;
|
|
var WebSocketServer = require('ws').Server;
|
|
var WebSocketServer = require('ws').Server;
|
|
var url = require('url');
|
|
var url = require('url');
|
|
|
|
|
|
|
|
+var allowCrossDomain = function(req, res, next) {
|
|
|
|
+ res.header('Access-Control-Allow-Origin', '*');
|
|
|
|
+ res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
|
|
|
|
+ res.header('Access-Control-Allow-Headers', 'Content-Type');
|
|
|
|
+ next();
|
|
|
|
+};
|
|
|
|
|
|
function PeerServer(options) {
|
|
function PeerServer(options) {
|
|
if (!(this instanceof PeerServer)) return new PeerServer(options);
|
|
if (!(this instanceof PeerServer)) return new PeerServer(options);
|
|
@@ -12,6 +18,11 @@ function PeerServer(options) {
|
|
|
|
|
|
this._app = express();
|
|
this._app = express();
|
|
this._httpServer = http.createServer(this._app);
|
|
this._httpServer = http.createServer(this._app);
|
|
|
|
+ var self = this;
|
|
|
|
+ this._app.configure(function() {
|
|
|
|
+ self._app.use(express.bodyParser());
|
|
|
|
+ self._app.use(allowCrossDomain);
|
|
|
|
+ });
|
|
|
|
|
|
options = util.extend({
|
|
options = util.extend({
|
|
port: 80
|
|
port: 80
|
|
@@ -74,7 +85,7 @@ PeerServer.prototype._initializeWSS = function() {
|
|
// Clean up.
|
|
// Clean up.
|
|
if (message.type === 'LEAVE') {
|
|
if (message.type === 'LEAVE') {
|
|
delete self._clients[message.src];
|
|
delete self._clients[message.src];
|
|
- delete self._requests[message.src];
|
|
|
|
|
|
+ delete self._timeouts[message.src];
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
@@ -95,7 +106,7 @@ PeerServer.prototype._initializeHTTP = function() {
|
|
// Retrieve guaranteed random ID.
|
|
// Retrieve guaranteed random ID.
|
|
this._app.get('/id', function(req, res) {
|
|
this._app.get('/id', function(req, res) {
|
|
var clientId = util.randomId();
|
|
var clientId = util.randomId();
|
|
- while (!!self._clients[clientId] || !!self._requests[clientId]) {
|
|
|
|
|
|
+ while (!!self._clients[clientId]) {
|
|
clientId = util.randomId();
|
|
clientId = util.randomId();
|
|
}
|
|
}
|
|
self._startStreaming(res, clientId, function() {
|
|
self._startStreaming(res, clientId, function() {
|
|
@@ -158,7 +169,7 @@ PeerServer.prototype._handleTransmission = function(type, src, dst, data) {
|
|
|
|
|
|
PeerServer.prototype._generateClientId = function() {
|
|
PeerServer.prototype._generateClientId = function() {
|
|
var clientId = util.randomId();
|
|
var clientId = util.randomId();
|
|
- while (!!self._clients[clientId] || !!self._requests[clientId]) {
|
|
|
|
|
|
+ while (!!self._clients[clientId]) {
|
|
clientId = util.randomId();
|
|
clientId = util.randomId();
|
|
}
|
|
}
|
|
};
|
|
};
|