-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
79 lines (65 loc) · 2.37 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
79
// Copyright 2016, EMC, Inc.
"use strict";
var flattenDeep = require('lodash.flattendeep'),
onCore = require('on-core'),
ws = require('ws');
module.exports = onWssContextFactory;
function onWssContextFactory() {
var core = onCore(),
helper = core.helper,
di = core.di;
return {
helper: helper,
injectables: [
helper.simpleWrapper(ws, 'ws'),
helper.simpleWrapper(ws.Server, 'WebSocketServer'),
helper.requireGlob(__dirname + '/lib/*.js')
],
initialize: function () {
var injector = new di.Injector(flattenDeep([
core.injectables,
this.injectables
]));
this.injector = injector;
this.logger = injector.get('Logger').initialize('Wss.Server');
this.messenger = injector.get('Services.Messenger');
this.waterline = injector.get('Services.Waterline');
this.wss = injector.get('Services.WebSocket');
return this;
},
run: function () {
var logger = this.logger,
messenger = this.messenger,
waterline = this.waterline,
wss = this.wss;
Promise.all([messenger.start(), waterline.start()])
.then(function () {
console.log('Messenger and Waterline Services started...');
wss.start(function (error) {
console.log('RackHD WebSocketServer started...');
if (error) {
console.error(error);
}
});
})
.catch(function (err) { console.error(err);});
process.on('SIGINT', function () {
Promise.all([messenger.stop(), waterline.stop()])
.then(function() {
wss.stop();
console.log('Stopping WebSocketServer...');
})
.catch(function(error) {
logger.critical('Server Shutdown Error.', { error: error });
process.nextTick(function() {
process.exit(1);
});
});
});
return this;
}
};
}
if (require.main === module) {
onWssContextFactory().initialize().run();
}