-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
133 lines (114 loc) · 4.44 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*jslint node:true */
"use strict";
var path = require('path');
var express = require('express');
var server = express();
var faye = require('faye');
var bayeuxMount = "/meet";
var cluster = require('cluster');
//-----------------------------------------------------------------------------
// Unique Room URL generation
//-----------------------------------------------------------------------------
// This is not intended for production use, and is simply for demonstration purposes.
var channelCounterMax = 4096;
var channelCounter1 = 0;
var channelCounter2 = 0;
var Hashids = require('hashids');
var roomHasher = new Hashids('SALT' + Math.random() * 100, 0, 'abcdefghijklmnopqrstuvwxyz');
function getUniqueMeetUrl() {
if(channelCounter1 === channelCounterMax){
channelCounter1 = 0;
channelCounter2++;
if(channelCounter2 >= channelCounterMax){
throw new Error("You've exhausted your _very_ considerable number of room counters.")
}
}
channelCounter1++;
var hash = roomHasher.encrypt([channelCounter1,channelCounter2]);
return hash;
}
// Heroku will specify the port to listen on with the `process.env.PORT` variable.
var serverPort = process.env.PORT || 4202;
// gzip scripts/css when possible.
server.use(express.compress());
// Pretty print HTML outputs in development and debug configurations
if(process.env.NODE_ENV !== 'production'){
server.locals.pretty = true;
}
// Error reporter
server.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
// Production Settings
server.configure('production', function(){
server.use(express.errorHandler());
});
//-----------------------------------------------------------------------------
// Express Routes
//-----------------------------------------------------------------------------
server.get('/', function(req,res){
res.redirect('/m/' + getUniqueMeetUrl());
});
server.get('/m/:id', function(req,res){
res.render('index.html',{
pubsubport:serverPort,
pubsubmount:bayeuxMount,
channel:req.params.id
});
});
// Use EJS templating with Express, and assign .html as the default extension.
server.engine('.html', require('ejs').__express);
server.set('view engine', 'html');
server.set('views',"public/");
// Mount the `public` directory for static file serving.
server.use(express.static(path.resolve(__dirname + "/public")));
server.use("/source", express.static(path.resolve(__dirname + "/source")));
var hServer = server.listen(serverPort);
console.log((cluster.isMaster ? "" : "[Worker" + cluster.worker.id + "]") + " Initialized on port " + serverPort);
//-----------------------------------------------------------------------------
// Configure Faye PubSub for peer messaging
//-----------------------------------------------------------------------------
!cluster.isMaster && console.log("[Worker" + cluster.worker.id + "] Initializing Faye...");
var fayeConfig = {
mount: bayeuxMount,
timeout: 45
};
// Support Redis via a fully qualified connection URL via `REDIS_URL`
// environment variable. Detect Heroku RedisToGo and RedisCloud add-ons.
if(process.env.REDIS_URL || process.env.REDISTOGO_URL || process.env.REDISCLOUD_URL){
try{
var url = require('url');
var uri = url.parse(process.env.REDIS_URL || process.env.REDISTOGO_URL || process.env.REDISCLOUD_URL);
var fayeRedis = require('faye-redis');
var redisName = process.env.REDISTOGO_URL ? "RedisToGo" : process.env.REDIS_URL ? "Redis" : "RedisCloud";
console.log(" -- using " + redisName + " adapter at (" + uri.hostname + ":" + uri.port + ")");
fayeConfig.engine = {
type: fayeRedis,
host: uri.hostname,
port: uri.port
};
// Authentication
if(uri.auth){
fayeConfig.engine.password = uri.auth.split(":")[1];
}
}
catch(e){
delete fayeConfig.engine;
console.log(" FAILED: " + e);
}
}
// Catch bad cluster configurations and exit the app.
//
// It's better to blow up on start than silently fail and leave you guessing.
if(!cluster.isMaster && typeof fayeConfig.engine === 'undefined'){
console.log([
"Cluster Configuration Error: Exiting",
"Cannot properly function in cluster mode without a remote storage adapter.",
"Recommend: Configure Redis or run in single process mode:\n node server.js."
].join('\n'));
process.exit(1);
}
// Initialize and attach faye to the express server.
var bayeux = new faye.NodeAdapter(fayeConfig);
bayeux.attach(hServer);