forked from rauchg/chat-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (68 loc) · 1.64 KB
/
index.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
var Hapi = require('hapi');
var Good = require('good');
var fs = require('fs');
var server = new Hapi.Server();
server.connection({ port: process.env.PORT || 3000, host: '0.0.0.0' });
var io = require('socket.io')(server.listener);
// Holds the history of the chat, so
// anyone joining can see the whole thing
var history = [];
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: __dirname
}
}
});
server.route({
method: 'GET',
path: '/',
handler: function(request, reply){
reply.file(__dirname + '/index.html');
}
});
server.route({
method: 'GET',
path: '/emojies',
handler: function(request, reply){
var emojiDescriptionsFilePath = __dirname + '/bower_components/emojione/emoji_strategy.json';
var emojiDescriptions = JSON.parse(fs.readFileSync(emojiDescriptionsFilePath, 'utf8'));
var descriptionsNoKeys = [];
for(key in emojiDescriptions) {
description = emojiDescriptions[key]
// We want to be able to use this as a lookup key - so replace
// colons when pushing up
description["shortname"] = description["shortname"].replace(/:/gi, "");
descriptionsNoKeys.push(description);
}
reply(descriptionsNoKeys);
}
});
server.register({
register: Good,
options: {
reporters: [{
reporter: require('good-console'),
events: {
response: '*',
log: '*',
}
}]
}
}, function (err) {
if (err) {
throw err;
}
});
io.on('connection', function(socket){
socket.emit('welcome', history);
socket.on('chat message', function(msg){
io.emit('chat message', msg);
history.push(msg);
});
});
server.start(function(){
server.log('listening at: ' + server.info.uri);
});