-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
196 lines (156 loc) · 5.47 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
var http = require('http');
var express = require('express');
var socketio = require('socket.io');
var async = require('async');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var winston = require('winston');
var ipfilter = require('express-ipfilter');
// Setup the logger
winston.addColors({
debug: 'green',
info: 'cyan',
silly: 'magenta',
warn: 'yellow',
error: 'red'
});
winston.add(winston.transports.File, {
name: 'info',
filename: './logs/info.log',
level: 'info',
colorize: true
});
winston.add(winston.transports.File, {
name: 'error',
filename: './logs/error.log',
level: 'error',
colorize: true
});
var youtubeApi = require('./api/youtube-api');
var twitchApi = require('./api/twitch-api');
var hitboxApi = require('./api/hitbox-api');
var beamApi = require('./api/beam-api');
var dailymotionApi = require('./api/dailymotion-api');
var chatMessageId = 0;
var chatMessages = [];
var systemMessages = [];
var maxMessagesStored = 100;
function run(config) {
// Initialize all APIs
if (config.live_data.youtube.enabled)
youtubeApi.initialize(config);
if (config.live_data.twitch.enabled)
twitchApi.initialize(config);
if (config.live_data.hitbox.enabled)
hitboxApi.initialize(config);
if (config.live_data.beam.enabled)
beamApi.initialize(config);
if (config.live_data.dailymotion.enabled)
dailymotionApi.initialize(config);
var app = express();
app.use(express.static('public'));
app.use(ipfilter(config.whitelisted_ips, {
mode: 'allow',
logF: winston.info
}));
var server = http.Server(app);
var io = socketio(server);
io.on('connection', function(socket) {
winston.info('Someone has connected to the chat');
socket.emit('connected');
// Send only the last 10 messages
chatMessagesToSend = chatMessages.slice(Math.max(chatMessages.length - 10, 0));
io.emit('oldChatMessages', chatMessagesToSend);
// Send system messages
io.emit('oldSystemMessages', systemMessages);
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/public/chat.html'));
});
if (config.live_data.youtube.enabled && config.live_data.youtube.redirect_url) {
app.get(config.live_data.youtube.redirect_url, function(req, res){
youtubeApi.getToken(req.query.code);
res.redirect('/');
});
}
server.listen(config.port, function() {
winston.info('listening on *: ' + config.port);
});
// Retrieve new messages
var newMessages = [];
async.forever(
function(next) {
if (config.live_data.youtube.enabled) {
youtubeApi.getNewMessages().forEach(function(elt) {
newMessages.push(elt);
});
}
if (config.live_data.twitch.enabled && twitchApi.isReady()) {
twitchApi.getNewMessages().forEach(function(elt) {
newMessages.push(elt);
});
}
if (config.live_data.hitbox.enabled && hitboxApi.isReady()) {
hitboxApi.getNewMessages().forEach(function(elt) {
newMessages.push(elt);
});
}
if (config.live_data.beam.enabled && beamApi.isReady()) {
beamApi.getNewMessages().forEach(function(elt) {
newMessages.push(elt);
});
}
if (config.live_data.dailymotion.enabled && dailymotionApi.isReady()) {
dailymotionApi.getNewMessages().forEach(function(elt) {
newMessages.push(elt);
});
}
if (newMessages.length > 0)
{
winston.info(newMessages);
newMessages.forEach(function(elt) {
if (elt.type == 'chat') {
// Affect a unique id to each message
elt.id = chatMessageId;
chatMessageId++;
chatMessages.push(elt);
// Make sure to keep less than the maximum allowed
if (chatMessages.length > maxMessagesStored)
chatMessages.shift();
io.emit('newChatMessage', elt);
}
else if (elt.type == 'system') {
systemMessages.push(elt);
if (systemMessages.length > maxMessagesStored)
systemMessages.shift();
io.emit('newSystemMessage', elt);
}
});
newMessages = [];
}
setTimeout(next, 1000);
},
function(err) {
winston.error('Error retrieving new messages: ' + err);
}
);
}
// Create a new directory for log files
mkdirp('./logs', function(err) {
if (err)
winston.error('Unable to create the log folder', err);
});
// Load config file
fs.readFile('config.json', function (err, config) {
if (err) {
winston.error('Error loading config file: ' + err);
return;
}
config = JSON.parse(config);
if (!config.host || !config.port) {
winston.error('Error loading config file: host or port is missing!');
return;
}
run(config);
});