-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (108 loc) · 4.25 KB
/
server.js
File metadata and controls
124 lines (108 loc) · 4.25 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
const http = require('http');
const eetase = require('eetase');
const socketClusterServer = require('socketcluster-server');
const express = require('express');
const serveStatic = require('serve-static');
const path = require('path');
const morgan = require('morgan');
const uuid = require('uuid');
const sccBrokerClient = require('scc-broker-client');
const ENVIRONMENT = process.env.ENV || 'dev';
const SOCKETCLUSTER_PORT = process.env.SOCKETCLUSTER_PORT || 8000;
const SOCKETCLUSTER_WS_ENGINE = process.env.SOCKETCLUSTER_WS_ENGINE || 'ws';
const SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT = Number(process.env.SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT) || 1000;
const SOCKETCLUSTER_LOG_LEVEL = process.env.SOCKETCLUSTER_LOG_LEVEL || 2;
const SCC_INSTANCE_ID = uuid.v4();
const SCC_STATE_SERVER_HOST = process.env.SCC_STATE_SERVER_HOST || null;
const SCC_STATE_SERVER_PORT = process.env.SCC_STATE_SERVER_PORT || null;
const SCC_MAPPING_ENGINE = process.env.SCC_MAPPING_ENGINE || null;
const SCC_CLIENT_POOL_SIZE = process.env.SCC_CLIENT_POOL_SIZE || null;
const SCC_AUTH_KEY = process.env.SCC_AUTH_KEY || null;
const SCC_INSTANCE_IP = process.env.SCC_INSTANCE_IP || null;
const SCC_INSTANCE_IP_FAMILY = process.env.SCC_INSTANCE_IP_FAMILY || null;
const SCC_STATE_SERVER_CONNECT_TIMEOUT = Number(process.env.SCC_STATE_SERVER_CONNECT_TIMEOUT) || null;
const SCC_STATE_SERVER_ACK_TIMEOUT = Number(process.env.SCC_STATE_SERVER_ACK_TIMEOUT) || null;
const SCC_STATE_SERVER_RECONNECT_RANDOMNESS = Number(process.env.SCC_STATE_SERVER_RECONNECT_RANDOMNESS) || null;
const SCC_PUB_SUB_BATCH_DURATION = Number(process.env.SCC_PUB_SUB_BATCH_DURATION) || null;
const SCC_BROKER_RETRY_DELAY = Number(process.env.SCC_BROKER_RETRY_DELAY) || null;
let agOptions = {};
if (process.env.SOCKETCLUSTER_OPTIONS) {
let envOptions = JSON.parse(process.env.SOCKETCLUSTER_OPTIONS);
Object.assign(agOptions, envOptions);
}
let httpServer = eetase(http.createServer());
let agServer = socketClusterServer.attach(httpServer, agOptions);
let expressApp = express();
if (ENVIRONMENT === 'dev') {
// Log every HTTP request. See https://github.com/expressjs/morgan for other
// available formats.
expressApp.use(morgan('dev'));
}
expressApp.use(serveStatic(path.resolve(__dirname, 'public')));
// Add GET /health-check express route
expressApp.get('/health-check', (req, res) => {
res.status(200).send('OK');
});
// HTTP request handling loop.
(async () => {
for await (let requestData of httpServer.listener('request')) {
expressApp.apply(null, requestData);
}
})();
// SocketCluster/WebSocket connection handling loop.
(async () => {
for await (let {socket} of agServer.listener('connection')) {
// Handle socket connection.
}
})();
httpServer.listen(SOCKETCLUSTER_PORT);
if (SOCKETCLUSTER_LOG_LEVEL >= 1) {
(async () => {
for await (let {error} of agServer.listener('error')) {
console.error(error);
}
})();
}
if (SOCKETCLUSTER_LOG_LEVEL >= 2) {
console.log(
` ${colorText('[Active]', 32)} SocketCluster worker with PID ${process.pid} is listening on port ${SOCKETCLUSTER_PORT}`
);
(async () => {
for await (let {warning} of agServer.listener('warning')) {
console.warn(warning);
}
})();
}
function colorText(message, color) {
if (color) {
return `\x1b[${color}m${message}\x1b[0m`;
}
return message;
}
if (SCC_STATE_SERVER_HOST) {
// Setup broker client to connect to SCC.
let sccClient = sccBrokerClient.attach(agServer.brokerEngine, {
instanceId: SCC_INSTANCE_ID,
instancePort: SOCKETCLUSTER_PORT,
instanceIp: SCC_INSTANCE_IP,
instanceIpFamily: SCC_INSTANCE_IP_FAMILY,
pubSubBatchDuration: SCC_PUB_SUB_BATCH_DURATION,
stateServerHost: SCC_STATE_SERVER_HOST,
stateServerPort: SCC_STATE_SERVER_PORT,
mappingEngine: SCC_MAPPING_ENGINE,
clientPoolSize: SCC_CLIENT_POOL_SIZE,
authKey: SCC_AUTH_KEY,
stateServerConnectTimeout: SCC_STATE_SERVER_CONNECT_TIMEOUT,
stateServerAckTimeout: SCC_STATE_SERVER_ACK_TIMEOUT,
stateServerReconnectRandomness: SCC_STATE_SERVER_RECONNECT_RANDOMNESS,
brokerRetryDelay: SCC_BROKER_RETRY_DELAY
});
if (SOCKETCLUSTER_LOG_LEVEL >= 1) {
(async () => {
for await (let {error} of sccClient.listener('error')) {
error.name = 'SCCError';
console.error(error);
}
})();
}
}