1
1
const http = require ( 'http' ) ;
2
2
const eetase = require ( 'eetase' ) ;
3
- const asyngularServer = require ( 'asyngular -server' ) ;
3
+ const socketClusterServer = require ( 'socketcluster -server' ) ;
4
4
const express = require ( 'express' ) ;
5
5
const serveStatic = require ( 'serve-static' ) ;
6
6
const path = require ( 'path' ) ;
7
7
const morgan = require ( 'morgan' ) ;
8
8
const uuid = require ( 'uuid' ) ;
9
- const agcBrokerClient = require ( 'agc -broker-client' ) ;
9
+ const sccBrokerClient = require ( 'scc -broker-client' ) ;
10
10
11
11
const ENVIRONMENT = process . env . ENV || 'dev' ;
12
- const ASYNGULAR_PORT = process . env . ASYNGULAR_PORT || 8000 ;
13
- const ASYNGULAR_WS_ENGINE = process . env . ASYNGULAR_WS_ENGINE || 'ws' ;
14
- const ASYNGULAR_SOCKET_CHANNEL_LIMIT = Number ( process . env . ASYNGULAR_SOCKET_CHANNEL_LIMIT ) || 1000 ;
15
- const ASYNGULAR_LOG_LEVEL = process . env . ASYNGULAR_LOG_LEVEL || 2 ;
16
-
17
- const AGC_INSTANCE_ID = uuid . v4 ( ) ;
18
- const AGC_STATE_SERVER_HOST = process . env . AGC_STATE_SERVER_HOST || null ;
19
- const AGC_STATE_SERVER_PORT = process . env . AGC_STATE_SERVER_PORT || null ;
20
- const AGC_MAPPING_ENGINE = process . env . AGC_MAPPING_ENGINE || null ;
21
- const AGC_CLIENT_POOL_SIZE = process . env . AGC_CLIENT_POOL_SIZE || null ;
22
- const AGC_AUTH_KEY = process . env . AGC_AUTH_KEY || null ;
23
- const AGC_INSTANCE_IP = process . env . AGC_INSTANCE_IP || null ;
24
- const AGC_INSTANCE_IP_FAMILY = process . env . AGC_INSTANCE_IP_FAMILY || null ;
25
- const AGC_STATE_SERVER_CONNECT_TIMEOUT = Number ( process . env . AGC_STATE_SERVER_CONNECT_TIMEOUT ) || null ;
26
- const AGC_STATE_SERVER_ACK_TIMEOUT = Number ( process . env . AGC_STATE_SERVER_ACK_TIMEOUT ) || null ;
27
- const AGC_STATE_SERVER_RECONNECT_RANDOMNESS = Number ( process . env . AGC_STATE_SERVER_RECONNECT_RANDOMNESS ) || null ;
28
- const AGC_PUB_SUB_BATCH_DURATION = Number ( process . env . AGC_PUB_SUB_BATCH_DURATION ) || null ;
29
- const AGC_BROKER_RETRY_DELAY = Number ( process . env . AGC_BROKER_RETRY_DELAY ) || null ;
12
+ const SOCKETCLUSTER_PORT = process . env . SOCKETCLUSTER_PORT || 8000 ;
13
+ const SOCKETCLUSTER_WS_ENGINE = process . env . SOCKETCLUSTER_WS_ENGINE || 'ws' ;
14
+ const SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT = Number ( process . env . SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT ) || 1000 ;
15
+ const SOCKETCLUSTER_LOG_LEVEL = process . env . SOCKETCLUSTER_LOG_LEVEL || 2 ;
16
+
17
+ const SCC_INSTANCE_ID = uuid . v4 ( ) ;
18
+ const SCC_STATE_SERVER_HOST = process . env . SCC_STATE_SERVER_HOST || null ;
19
+ const SCC_STATE_SERVER_PORT = process . env . SCC_STATE_SERVER_PORT || null ;
20
+ const SCC_MAPPING_ENGINE = process . env . SCC_MAPPING_ENGINE || null ;
21
+ const SCC_CLIENT_POOL_SIZE = process . env . SCC_CLIENT_POOL_SIZE || null ;
22
+ const SCC_AUTH_KEY = process . env . SCC_AUTH_KEY || null ;
23
+ const SCC_INSTANCE_IP = process . env . SCC_INSTANCE_IP || null ;
24
+ const SCC_INSTANCE_IP_FAMILY = process . env . SCC_INSTANCE_IP_FAMILY || null ;
25
+ const SCC_STATE_SERVER_CONNECT_TIMEOUT = Number ( process . env . SCC_STATE_SERVER_CONNECT_TIMEOUT ) || null ;
26
+ const SCC_STATE_SERVER_ACK_TIMEOUT = Number ( process . env . SCC_STATE_SERVER_ACK_TIMEOUT ) || null ;
27
+ const SCC_STATE_SERVER_RECONNECT_RANDOMNESS = Number ( process . env . SCC_STATE_SERVER_RECONNECT_RANDOMNESS ) || null ;
28
+ const SCC_PUB_SUB_BATCH_DURATION = Number ( process . env . SCC_PUB_SUB_BATCH_DURATION ) || null ;
29
+ const SCC_BROKER_RETRY_DELAY = Number ( process . env . SCC_BROKER_RETRY_DELAY ) || null ;
30
30
31
31
let agOptions ;
32
32
33
- if ( process . env . ASYNGULAR_OPTIONS ) {
34
- agOptions = JSON . parse ( process . env . ASYNGULAR_OPTIONS ) ;
33
+ if ( process . env . SOCKETCLUSTER_OPTIONS ) {
34
+ agOptions = JSON . parse ( process . env . SOCKETCLUSTER_OPTIONS ) ;
35
35
} else {
36
36
agOptions = { } ;
37
37
}
38
38
39
39
let httpServer = eetase ( http . createServer ( ) ) ;
40
- let agServer = asyngularServer . attach ( httpServer , agOptions ) ;
40
+ let agServer = socketClusterServer . attach ( httpServer , agOptions ) ;
41
41
42
42
let expressApp = express ( ) ;
43
43
if ( ENVIRONMENT === 'dev' ) {
@@ -59,17 +59,17 @@ expressApp.get('/health-check', (req, res) => {
59
59
}
60
60
} ) ( ) ;
61
61
62
- if ( ASYNGULAR_LOG_LEVEL >= 1 ) {
62
+ if ( SOCKETCLUSTER_LOG_LEVEL >= 1 ) {
63
63
( async ( ) => {
64
64
for await ( let { error} of agServer . listener ( 'error' ) ) {
65
65
console . error ( error ) ;
66
66
}
67
67
} ) ( ) ;
68
68
}
69
69
70
- if ( ASYNGULAR_LOG_LEVEL >= 2 ) {
70
+ if ( SOCKETCLUSTER_LOG_LEVEL >= 2 ) {
71
71
console . log (
72
- ` ${ colorText ( '[Active]' , 32 ) } Asyngular worker with PID ${ process . pid } is listening on port ${ ASYNGULAR_PORT } `
72
+ ` ${ colorText ( '[Active]' , 32 ) } SocketCluster worker with PID ${ process . pid } is listening on port ${ SOCKETCLUSTER_PORT } `
73
73
) ;
74
74
75
75
( async ( ) => {
@@ -88,29 +88,29 @@ function colorText(message, color) {
88
88
89
89
let stats = { } ;
90
90
91
- if ( AGC_STATE_SERVER_HOST ) {
92
- // Setup broker client to connect to the Asyngular cluster (AGC ).
93
- let agcClient = agcBrokerClient . attach ( agServer . brokerEngine , {
94
- instanceId : AGC_INSTANCE_ID ,
95
- instancePort : ASYNGULAR_PORT ,
96
- instanceIp : AGC_INSTANCE_IP ,
97
- instanceIpFamily : AGC_INSTANCE_IP_FAMILY ,
98
- pubSubBatchDuration : AGC_PUB_SUB_BATCH_DURATION ,
99
- stateServerHost : AGC_STATE_SERVER_HOST ,
100
- stateServerPort : AGC_STATE_SERVER_PORT ,
101
- mappingEngine : AGC_MAPPING_ENGINE ,
102
- clientPoolSize : AGC_CLIENT_POOL_SIZE ,
103
- authKey : AGC_AUTH_KEY ,
104
- stateServerConnectTimeout : AGC_STATE_SERVER_CONNECT_TIMEOUT ,
105
- stateServerAckTimeout : AGC_STATE_SERVER_ACK_TIMEOUT ,
106
- stateServerReconnectRandomness : AGC_STATE_SERVER_RECONNECT_RANDOMNESS ,
107
- brokerRetryDelay : AGC_BROKER_RETRY_DELAY
91
+ if ( SCC_STATE_SERVER_HOST ) {
92
+ // Setup broker client to connect to the SocketCluster cluster (SCC ).
93
+ let sccClient = sccBrokerClient . attach ( agServer . brokerEngine , {
94
+ instanceId : SCC_INSTANCE_ID ,
95
+ instancePort : SOCKETCLUSTER_PORT ,
96
+ instanceIp : SCC_INSTANCE_IP ,
97
+ instanceIpFamily : SCC_INSTANCE_IP_FAMILY ,
98
+ pubSubBatchDuration : SCC_PUB_SUB_BATCH_DURATION ,
99
+ stateServerHost : SCC_STATE_SERVER_HOST ,
100
+ stateServerPort : SCC_STATE_SERVER_PORT ,
101
+ mappingEngine : SCC_MAPPING_ENGINE ,
102
+ clientPoolSize : SCC_CLIENT_POOL_SIZE ,
103
+ authKey : SCC_AUTH_KEY ,
104
+ stateServerConnectTimeout : SCC_STATE_SERVER_CONNECT_TIMEOUT ,
105
+ stateServerAckTimeout : SCC_STATE_SERVER_ACK_TIMEOUT ,
106
+ stateServerReconnectRandomness : SCC_STATE_SERVER_RECONNECT_RANDOMNESS ,
107
+ brokerRetryDelay : SCC_BROKER_RETRY_DELAY
108
108
} ) ;
109
109
110
- if ( ASYNGULAR_LOG_LEVEL >= 1 ) {
110
+ if ( SOCKETCLUSTER_LOG_LEVEL >= 1 ) {
111
111
( async ( ) => {
112
- for await ( let { error} of agcClient . listener ( 'error' ) ) {
113
- error . name = 'AGCError ' ;
112
+ for await ( let { error} of sccClient . listener ( 'error' ) ) {
113
+ error . name = 'SCCError ' ;
114
114
console . error ( error ) ;
115
115
}
116
116
} ) ( ) ;
@@ -135,33 +135,33 @@ if (AGC_STATE_SERVER_HOST) {
135
135
}
136
136
137
137
( async ( ) => {
138
- for await ( let data of agcClient . listener ( 'subscribe' ) ) {
138
+ for await ( let data of sccClient . listener ( 'subscribe' ) ) {
139
139
handleEventData ( 'subscribe' , data ) ;
140
140
}
141
141
} ) ( ) ;
142
142
143
143
( async ( ) => {
144
- for await ( let data of agcClient . listener ( 'subscribeFail' ) ) {
144
+ for await ( let data of sccClient . listener ( 'subscribeFail' ) ) {
145
145
handleEventData ( 'subscribeFail' , data ) ;
146
146
}
147
147
} ) ( ) ;
148
148
149
149
( async ( ) => {
150
- for await ( let data of agcClient . listener ( 'publish' ) ) {
150
+ for await ( let data of sccClient . listener ( 'publish' ) ) {
151
151
handleEventData ( 'publish' , data ) ;
152
152
}
153
153
} ) ( ) ;
154
154
155
155
( async ( ) => {
156
- for await ( let data of agcClient . listener ( 'publishFail' ) ) {
156
+ for await ( let data of sccClient . listener ( 'publishFail' ) ) {
157
157
handleEventData ( 'publishFail' , data ) ;
158
158
}
159
159
} ) ( ) ;
160
160
161
161
// ---- End stats collections ----
162
162
}
163
163
164
- // Asyngular /WebSocket connection handling loop.
164
+ // SocketCluster /WebSocket connection handling loop.
165
165
( async ( ) => {
166
166
for await ( let { socket} of agServer . listener ( 'connection' ) ) {
167
167
// Handle socket connection.
@@ -182,4 +182,4 @@ if (AGC_STATE_SERVER_HOST) {
182
182
}
183
183
} ) ( ) ;
184
184
185
- httpServer . listen ( ASYNGULAR_PORT ) ;
185
+ httpServer . listen ( SOCKETCLUSTER_PORT ) ;
0 commit comments