forked from nko4/sophilabs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
202 lines (174 loc) · 4.54 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var express = require('express');
var http = require('http');
var redis = require('redis');
var redisUrl = require('redis-url');
var fs = require('fs');
var ws = require('ws');
var url = require('url');
var GIFEncoder = require('./lib/GIFEncoder.js');
// Common settings
var common = require('./common.js');
var redisCreateClient = function() {
if (process.env.REDISTOGO_URL) {
return redisUrl.connect(process.env.REDISTOGO_URL);
} else {
return redis.createClient();
}
};
// Create app and websockets
var app = express();
var server = http.createServer(app);
var client = redisCreateClient();
var sockets = new ws.Server({
server: server
});
// Port
var isProduction = (process.env.NODE_ENV === 'production');
var port = (isProduction ? 80 : 8000);
server.listen(port);
/*
* Load images.
*/
var loadRAWImage = function(name) {
var path = __dirname + '/res/' + name + '-' +
common.WIDTH + 'x' + common.HEIGHT + '.json';
var data = fs.readFileSync(path, 'utf8');
return JSON.parse(data);
};
var adjustment = loadRAWImage('adjustment');
var establishing = loadRAWImage('establishing');
/*
* Configure express app.
*/
app.configure(function() {
app.set('views', __dirname + '/templates');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'topsecret' }));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/static'));
app.use(express.favicon(__dirname + '/static/img/favicon.ico'));
});
/**
* Redirect to index.htm
*/
app.get('/', function(req, res) {
res.redirect('/index.htm');
});
/**
* Index
*/
app.get('/index.htm', function(req, res) {
res.render('app.jade', {});
});
/**
* Why
*/
app.get('/why.htm', function(req, res) {
res.render('why.jade', {});
});
/**
* Really
*/
app.get('/really.htm', function(req, res) {
res.render('really.jade', {});
});
/**
* About
*/
app.get('/about.htm', function(req, res) {
res.render('about.jade', {});
});
/**
* FAQ
*/
app.get('/faq.htm', function(req, res) {
res.render('faq.jade', {});
});
/**
* Watch a stream.
*/
app.get('/:id.gif', function(req, res) {
var client = redisCreateClient();
var encoder = new GIFEncoder(common.WIDTH, common.HEIGHT);
res.setHeader('Content-Type', 'image/gif');
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
/*
* Write GIF header.
*/
encoder.stream().onWrite(function(data) {
res.write(String.fromCharCode(data), 'binary');
});
encoder.setFrameRate(common.RECV_FRAMERATE);
encoder.writeHeader();
encoder.writeLSD(); // logical screen descriptior
encoder.writeGlobalPalette();
encoder.addFrame(establishing);
/*
* Read frames from Redis channel.
*/
client.psubscribe('*.' + req.params.id);
client.on('pmessage', function(pattern, channel, data) {
channel = channel.split('.');
var type = channel[0];
var gifId = channel[1];
if (type == common.redis.CH_FRAME) {
console.log('Received frame');
res.write(data, 'binary');
} else if (type == common.redis.CH_EVENT) {
console.log('Received event: ' + data);
if (data == common.redis.EVT_DISCONNECT) {
encoder.addFrame(adjustment);
res.end();
}
}
});
/*
* Close output stream.
*/
req.connection.addListener('close', function() {
client.unsubscribe();
client.end();
});
});
/**
* Generate an id using A-Z,a-z,0-9.
*/
var generateId = function() {
var id = '';
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++) {
id += possible.charAt(Math.floor(Math.random() * possible.length));
}
return id;
};
/**
* Setup new socket connection.
*/
sockets.on('connection', function(socket) {
var gifId = generateId();
socket.send(String.fromCharCode(common.events.EVT_NEW_ID) + gifId);
/*
* Receive frames and write to Redis channel.
*/
socket.on('message', function(data) {
var evt = data[0].charCodeAt(0);
var data = data.substr(1);
if (evt == common.events.EVT_FRAME) {
client.publish(common.redis.CH_FRAME + '.' + gifId, data);
socket.send(String.fromCharCode(common.events.EVT_FRAME_RECEIVED));
}
});
/*
* Close input stream.
*/
socket.on('close', function() {
client.publish(common.redis.CH_EVENT + '.' + gifId,
common.redis.EVT_DISCONNECT);
});
});