-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotifyit.js
More file actions
147 lines (124 loc) · 3.21 KB
/
notifyit.js
File metadata and controls
147 lines (124 loc) · 3.21 KB
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
'use strict';
var PORT = 2012,
express = require('express'),
crypto = require('crypto'),
hostname = require('os').hostname(),
colors = require('colors'),
path = require('path'),
app = express.createServer(),
io = require('socket.io'),
_ = require('underscore'),
homeFolder, port;
/**
* Constructor
*/
function NotifyIt( port ) {
initEnvironment( port );
initRouting();
initSocketIO();
/**
* Setting up environment
*/
function initEnvironment( serverPort ) {
homeFolder = __dirname;
console.log(' info -'.cyan, 'NotifyIt root'.yellow, homeFolder);
// express config
app.set('view engine', 'ejs');
app.set('views', homeFolder + '/views');
app.set('views');
app.set('view options', { layout: null });
app.use (function(req, res, next) {
var data='';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
// static resources
app.use('/js', express.static(homeFolder + '/js'));
app.use('/css', express.static(homeFolder + '/css'));
app.use('/images', express.static(homeFolder + '/images'));
// port
port = serverPort || parseInt(process.argv[2], 10) || PORT;
}
/**
* Init service routing
*/
function initRouting() {
/** Index page. */
app.get('/', function(request, response) {
response.render('index', { host: hostname, port: port });
});
/** Post new result */
app.post('/pub/:channel/:eventName', routePubRequest );
}
/**
* Handle HTTP request to publish data
*/
function routePubRequest(request, response) {
var data = request.body,
channel = request.params.channel,
eventName = request.params.eventName,
obj;
if(request.is('json')) {
try {
obj = JSON.parse(data);
} catch(e) {
response.send('Malformed JSON', 500);
}
} else {
obj = data;
}
if(obj) {
publish( channel, eventName, obj );
}
response.send(200);
}
/**
* Publish data to listeners
*/
function publish( channel, eventName, obj ) {
var evt = channel + ':' + eventName,
wrapper = {
channel: channel,
eventName: eventName,
data: obj
};
console.log(' info -'.cyan, 'publishing event'.white, evt.yellow, 'with data'.white, obj.toString().yellow);
io.sockets.emit(evt, wrapper);
io.sockets.emit('all', wrapper);
}
/**
* Socket io initialization
*/
function initSocketIO() {
io = io.listen(app);
io.set('log level', 1);
io.sockets.on('connection', function onConnection(socket) {
socket.emit('connected');
});
}
/**
* Start server
*/
function start() {
app.listen(port);
console.log('NotifyIt started on'.yellow, (hostname + ':' + port).cyan);
}
/**
* Stop server
*/
function stop() {
app.close();
console.log('NotifyIt shutting down'.yellow);
}
return {
start: start,
stop: stop
};
}
module.exports = NotifyIt;