podasync-ws-only 2.7.9 → 2.7.10-snapshot.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +6 -2
- package/src/network/async.js +52 -21
- package/src/network/socket.js +81 -65
- package/src/utility/logger.js +34 -0
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
First you have to require PodAsync in your project.
|
|
8
8
|
|
|
9
9
|
```javascript
|
|
10
|
-
var Async = require('podasync');
|
|
10
|
+
var Async = require('podasync-ws-only');
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
To be able to connect to async server, you should set some parameters. `Websockets`protocol is currently supported.
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "podasync-ws-only",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.10-snapshot.10",
|
|
4
4
|
"description": "Fanap's POD Async service (DIRANA) - Websocket only",
|
|
5
5
|
"main": "./src/network/async.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "mocha --reporter spec --exit"
|
|
7
|
+
"test": "mocha --reporter spec --exit",
|
|
8
|
+
"publish:snapshot": "npm run version:snapshot && npm publish --tag snapshot",
|
|
9
|
+
"version:snapshot": "npm version prerelease --preid snapshot",
|
|
10
|
+
"publish:release": "npm run version:release && npm publish",
|
|
11
|
+
"version:release": "npm version 2.7.8"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
package/src/network/async.js
CHANGED
|
@@ -13,15 +13,17 @@
|
|
|
13
13
|
*******************************************************/
|
|
14
14
|
|
|
15
15
|
var PodSocketClass,
|
|
16
|
-
PodUtility
|
|
17
|
-
|
|
16
|
+
PodUtility,
|
|
17
|
+
LogLevel
|
|
18
18
|
if (typeof(require) !== 'undefined' && typeof(exports) !== 'undefined') {
|
|
19
19
|
PodSocketClass = require('./socket.js');
|
|
20
20
|
PodUtility = require('../utility/utility.js');
|
|
21
|
+
LogLevel = require('../utility/logger.js');
|
|
21
22
|
}
|
|
22
23
|
else {
|
|
23
24
|
PodSocketClass = POD.Socket;
|
|
24
25
|
PodUtility = POD.AsyncUtility;
|
|
26
|
+
LogLevel = POD.LogLevel;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
var Utility = new PodUtility();
|
|
@@ -60,6 +62,7 @@
|
|
|
60
62
|
CLOSING: 2, // The connection is in the process of closing.
|
|
61
63
|
CLOSED: 3 // The connection is closed or couldn't be opened.
|
|
62
64
|
},
|
|
65
|
+
logLevel = LogLevel(params.logLevel),
|
|
63
66
|
isNode = Utility.isNode(),
|
|
64
67
|
isSocketOpen = false,
|
|
65
68
|
isDeviceRegister = false,
|
|
@@ -80,7 +83,7 @@
|
|
|
80
83
|
connectionRetryInterval = params.connectionRetryInterval || 5000,
|
|
81
84
|
socketReconnectRetryInterval,
|
|
82
85
|
socketReconnectCheck,
|
|
83
|
-
retryStep = 4,
|
|
86
|
+
// retryStep = 4,
|
|
84
87
|
reconnectOnClose = (typeof params.reconnectOnClose === 'boolean') ? params.reconnectOnClose : true,
|
|
85
88
|
asyncLogging = (params.asyncLogging && typeof params.asyncLogging.onFunction === 'boolean') ? params.asyncLogging.onFunction : false,
|
|
86
89
|
onReceiveLogging = (params.asyncLogging && typeof params.asyncLogging.onMessageReceive === 'boolean')
|
|
@@ -89,6 +92,27 @@
|
|
|
89
92
|
onSendLogging = (params.asyncLogging && typeof params.asyncLogging.onMessageSend === 'boolean') ? params.asyncLogging.onMessageSend : false,
|
|
90
93
|
workerId = (params.asyncLogging && typeof parseInt(params.asyncLogging.workerId) === 'number') ? params.asyncLogging.workerId : 0;
|
|
91
94
|
|
|
95
|
+
// function setRetryStep(val){
|
|
96
|
+
// console.log("new retryStep value:", val);
|
|
97
|
+
// retryStep = val;
|
|
98
|
+
// }
|
|
99
|
+
//
|
|
100
|
+
// function getRetryStep() {
|
|
101
|
+
// return retryStep;
|
|
102
|
+
// }
|
|
103
|
+
|
|
104
|
+
const retryStep = {
|
|
105
|
+
value: 4,
|
|
106
|
+
get() {
|
|
107
|
+
return retryStep.value;
|
|
108
|
+
},
|
|
109
|
+
set(val) {
|
|
110
|
+
// logLevel.debug && console.debug("[Async][async.js] retryStep new value:", val);
|
|
111
|
+
console.debug("[Async][async.js] retryStep new value:", val);
|
|
112
|
+
retryStep.value = val;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
92
116
|
/*******************************************************
|
|
93
117
|
* P R I V A T E M E T H O D S *
|
|
94
118
|
*******************************************************/
|
|
@@ -122,7 +146,8 @@
|
|
|
122
146
|
socketAddress: params.socketAddress,
|
|
123
147
|
wsConnectionWaitTime: params.wsConnectionWaitTime,
|
|
124
148
|
connectionCheckTimeout: params.connectionCheckTimeout,
|
|
125
|
-
connectionCheckTimeoutThreshold: params.connectionCheckTimeoutThreshold
|
|
149
|
+
connectionCheckTimeoutThreshold: params.connectionCheckTimeoutThreshold,
|
|
150
|
+
logLevel: logLevel
|
|
126
151
|
});
|
|
127
152
|
|
|
128
153
|
checkIfSocketHasOpennedTimeoutId = setTimeout(function () {
|
|
@@ -140,7 +165,8 @@
|
|
|
140
165
|
socketReconnectCheck && clearTimeout(socketReconnectCheck);
|
|
141
166
|
|
|
142
167
|
isSocketOpen = true;
|
|
143
|
-
retryStep = 4;
|
|
168
|
+
// retryStep = 4;
|
|
169
|
+
retryStep.set(4);
|
|
144
170
|
|
|
145
171
|
socketState = socketStateType.OPEN;
|
|
146
172
|
fireEvent('stateChange', {
|
|
@@ -164,32 +190,32 @@
|
|
|
164
190
|
isDeviceRegister = false;
|
|
165
191
|
oldPeerId = peerId;
|
|
166
192
|
|
|
167
|
-
socketState = socketStateType.CLOSED;
|
|
168
|
-
|
|
169
|
-
fireEvent('stateChange', {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
193
|
+
// socketState = socketStateType.CLOSED;
|
|
194
|
+
//
|
|
195
|
+
// fireEvent('stateChange', {
|
|
196
|
+
// socketState: socketState,
|
|
197
|
+
// timeUntilReconnect: 0,
|
|
198
|
+
// deviceRegister: isDeviceRegister,
|
|
199
|
+
// serverRegister: isServerRegister,
|
|
200
|
+
// peerId: peerId
|
|
201
|
+
// });
|
|
176
202
|
|
|
177
203
|
fireEvent('disconnect', event);
|
|
178
204
|
|
|
179
205
|
if (reconnectOnClose) {
|
|
180
206
|
if (asyncLogging) {
|
|
181
207
|
if (workerId > 0) {
|
|
182
|
-
Utility.asyncStepLogger(workerId + '\t Reconnecting after ' + retryStep + 's');
|
|
208
|
+
Utility.asyncStepLogger(workerId + '\t Reconnecting after ' + retryStep.get() + 's');
|
|
183
209
|
}
|
|
184
210
|
else {
|
|
185
|
-
Utility.asyncStepLogger('Reconnecting after ' + retryStep + 's');
|
|
211
|
+
Utility.asyncStepLogger('Reconnecting after ' + retryStep.get() + 's');
|
|
186
212
|
}
|
|
187
213
|
}
|
|
188
214
|
|
|
189
215
|
socketState = socketStateType.CLOSED;
|
|
190
216
|
fireEvent('stateChange', {
|
|
191
217
|
socketState: socketState,
|
|
192
|
-
timeUntilReconnect: 1000 * retryStep,
|
|
218
|
+
timeUntilReconnect: 1000 * retryStep.get(),
|
|
193
219
|
deviceRegister: isDeviceRegister,
|
|
194
220
|
serverRegister: isServerRegister,
|
|
195
221
|
peerId: peerId
|
|
@@ -199,10 +225,11 @@
|
|
|
199
225
|
|
|
200
226
|
socketReconnectRetryInterval = setTimeout(function () {
|
|
201
227
|
socket.connect();
|
|
202
|
-
}, 1000 * retryStep);
|
|
228
|
+
}, 1000 * retryStep.get());
|
|
203
229
|
|
|
204
|
-
if (retryStep < 64) {
|
|
205
|
-
retryStep
|
|
230
|
+
if (retryStep.get() < 64) {
|
|
231
|
+
// retryStep += 3;
|
|
232
|
+
retryStep.set(retryStep.get() + 3)
|
|
206
233
|
}
|
|
207
234
|
|
|
208
235
|
// socketReconnectCheck && clearTimeout(socketReconnectCheck);
|
|
@@ -356,9 +383,12 @@
|
|
|
356
383
|
|
|
357
384
|
if (peerId !== undefined) {
|
|
358
385
|
content.refresh = true;
|
|
386
|
+
content.renew = false;
|
|
387
|
+
|
|
359
388
|
}
|
|
360
389
|
else {
|
|
361
390
|
content.renew = true;
|
|
391
|
+
content.refresh = false;
|
|
362
392
|
}
|
|
363
393
|
|
|
364
394
|
pushSendData({
|
|
@@ -689,7 +719,8 @@
|
|
|
689
719
|
socket.close();
|
|
690
720
|
|
|
691
721
|
socketReconnectRetryInterval = setTimeout(function () {
|
|
692
|
-
retryStep = 4;
|
|
722
|
+
// retryStep = 4;
|
|
723
|
+
retryStep.set(4);
|
|
693
724
|
socket.connect();
|
|
694
725
|
}, 2000);
|
|
695
726
|
};
|
package/src/network/socket.js
CHANGED
|
@@ -22,12 +22,53 @@
|
|
|
22
22
|
eventCallback = {},
|
|
23
23
|
socket,
|
|
24
24
|
waitForSocketToConnectTimeoutId,
|
|
25
|
-
forceCloseSocket = false,
|
|
26
|
-
forceCloseSocketTimeout,
|
|
27
25
|
socketRealTimeStatusInterval,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
logLevel = params.logLevel,
|
|
27
|
+
pingController = new PingManager({waitTime: connectionCheckTimeout}),
|
|
28
|
+
socketWatchTimeout;
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
function PingManager(params) {
|
|
32
|
+
const config = {
|
|
33
|
+
normalWaitTime: params.waitTime,
|
|
34
|
+
|
|
35
|
+
lastRequestTimeoutId: null,
|
|
36
|
+
lastReceivedMessageTime: 0,
|
|
37
|
+
totalNoMessageCount: 0,
|
|
38
|
+
timeoutIds: {
|
|
39
|
+
first: null,
|
|
40
|
+
second: null,
|
|
41
|
+
third: null,
|
|
42
|
+
fourth: null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
resetPingLoop() {
|
|
48
|
+
this.stopPingLoop();
|
|
49
|
+
this.setPingTimeout();
|
|
50
|
+
},
|
|
51
|
+
setPingTimeout() {
|
|
52
|
+
config.timeoutIds.first = setTimeout(()=>{
|
|
53
|
+
ping();
|
|
54
|
+
config.timeoutIds.first = setTimeout(()=>{
|
|
55
|
+
ping();
|
|
56
|
+
config.timeoutIds.fourth = setTimeout(()=>{
|
|
57
|
+
logLevel.debug && console.debug("[Async][Socket.js] Force closing socket.");
|
|
58
|
+
onCloseHandler(null);
|
|
59
|
+
socket.close();
|
|
60
|
+
}, 2000);
|
|
61
|
+
}, 2000);
|
|
62
|
+
}, 8000);
|
|
63
|
+
},
|
|
64
|
+
stopPingLoop(){
|
|
65
|
+
clearTimeout(config.timeoutIds.first);
|
|
66
|
+
clearTimeout(config.timeoutIds.second);
|
|
67
|
+
clearTimeout(config.timeoutIds.third);
|
|
68
|
+
clearTimeout(config.timeoutIds.fourth);
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
31
72
|
|
|
32
73
|
/*******************************************************
|
|
33
74
|
* P R I V A T E M E T H O D S *
|
|
@@ -45,74 +86,57 @@
|
|
|
45
86
|
|
|
46
87
|
socket = new WebSocket(address, []);
|
|
47
88
|
|
|
48
|
-
socketRealTimeStatusInterval && clearInterval(socketRealTimeStatusInterval);
|
|
49
|
-
socketRealTimeStatusInterval = setInterval(function() {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
89
|
+
// socketRealTimeStatusInterval && clearInterval(socketRealTimeStatusInterval);
|
|
90
|
+
// socketRealTimeStatusInterval = setInterval(function() {
|
|
91
|
+
// switch (socket.readyState) {
|
|
92
|
+
// case 2:
|
|
93
|
+
// onCloseHandler(null);
|
|
94
|
+
// socketRealTimeStatusInterval && clearInterval(socketRealTimeStatusInterval);
|
|
95
|
+
// break;
|
|
96
|
+
// case 3:
|
|
97
|
+
//
|
|
98
|
+
// break;
|
|
99
|
+
// }
|
|
100
|
+
// }, 5000);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Watches the socket to make sure it's state changes to 1 in 5 seconds
|
|
104
|
+
*/
|
|
105
|
+
socketWatchTimeout && clearTimeout(socketWatchTimeout);
|
|
106
|
+
socketWatchTimeout = setTimeout(() => {
|
|
107
|
+
// if(socket.readyState !== 1) {
|
|
108
|
+
logLevel.debug && console.debug("[Async][Socket.js] socketWatchTimeout triggered.");
|
|
109
|
+
onCloseHandler(null);
|
|
110
|
+
socket.close();
|
|
111
|
+
// }
|
|
58
112
|
}, 5000);
|
|
59
113
|
|
|
60
114
|
socket.onopen = function(event) {
|
|
61
115
|
waitForSocketToConnect(function() {
|
|
116
|
+
pingController.resetPingLoop();
|
|
62
117
|
eventCallback["open"]();
|
|
118
|
+
socketWatchTimeout && clearTimeout(socketWatchTimeout);
|
|
63
119
|
});
|
|
64
120
|
}
|
|
65
121
|
|
|
66
122
|
socket.onmessage = function(event) {
|
|
123
|
+
pingController.resetPingLoop();
|
|
124
|
+
|
|
67
125
|
var messageData = JSON.parse(event.data);
|
|
68
126
|
eventCallback["message"](messageData);
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* To avoid manually closing socket's connection
|
|
72
|
-
*/
|
|
73
|
-
forceCloseSocket = false;
|
|
74
|
-
|
|
75
|
-
socketCloseTimeout && clearTimeout(socketCloseTimeout);
|
|
76
|
-
forceCloseTimeout && clearTimeout(forceCloseTimeout);
|
|
77
|
-
|
|
78
|
-
socketCloseTimeout = setTimeout(function() {
|
|
79
|
-
/**
|
|
80
|
-
* If message's type is not 5, socket won't get any acknowledge packet,therefore
|
|
81
|
-
* you may think that connection has been closed and you would force socket
|
|
82
|
-
* to close, but before that you should make sure that connection is actually closed!
|
|
83
|
-
* for that, you must send a ping message and if that message don't get any
|
|
84
|
-
* responses too, you are allowed to manually kill socket connection.
|
|
85
|
-
*/
|
|
86
|
-
ping();
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* We set forceCloseSocket as true so that if your ping's response don't make it
|
|
90
|
-
* you close your socket
|
|
91
|
-
*/
|
|
92
|
-
forceCloseSocket = true;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* If type of messages are not 5, you won't get ant ACK packets
|
|
96
|
-
* for that being said, we send a ping message to be sure of
|
|
97
|
-
* socket connection's state. The ping message should have an
|
|
98
|
-
* ACK, if not, you're allowed to close your socket after
|
|
99
|
-
* 4 * [connectionCheckTimeout] seconds
|
|
100
|
-
*/
|
|
101
|
-
forceCloseTimeout = setTimeout(function() {
|
|
102
|
-
if (forceCloseSocket) {
|
|
103
|
-
socket.close();
|
|
104
|
-
}
|
|
105
|
-
}, connectionCheckTimeout);
|
|
106
|
-
|
|
107
|
-
}, connectionCheckTimeout * 1.5);
|
|
108
127
|
}
|
|
109
128
|
|
|
110
129
|
socket.onclose = function(event) {
|
|
130
|
+
pingController.stopPingLoop();
|
|
131
|
+
logLevel.debug && console.debug("[Async][Socket.js] socket.onclose happened. EventData:", event);
|
|
111
132
|
onCloseHandler(event);
|
|
133
|
+
socketWatchTimeout && clearTimeout(socketWatchTimeout);
|
|
112
134
|
}
|
|
113
135
|
|
|
114
136
|
socket.onerror = function(event) {
|
|
137
|
+
logLevel.debug && console.debug("[Async][Socket.js] socket.onerror happened. EventData:", event);
|
|
115
138
|
eventCallback["error"](event);
|
|
139
|
+
socketWatchTimeout && clearTimeout(socketWatchTimeout);
|
|
116
140
|
}
|
|
117
141
|
} catch (error) {
|
|
118
142
|
eventCallback["customError"]({
|
|
@@ -124,9 +148,7 @@
|
|
|
124
148
|
},
|
|
125
149
|
|
|
126
150
|
onCloseHandler = function(event) {
|
|
127
|
-
|
|
128
|
-
socketCloseTimeout && clearTimeout(socketCloseTimeout);
|
|
129
|
-
forceCloseTimeout && clearTimeout(forceCloseTimeout);
|
|
151
|
+
pingController.stopPingLoop();
|
|
130
152
|
eventCallback["close"](event);
|
|
131
153
|
},
|
|
132
154
|
|
|
@@ -161,11 +183,6 @@
|
|
|
161
183
|
data.trackerId = params.trackerId;
|
|
162
184
|
}
|
|
163
185
|
|
|
164
|
-
sendPingTimeout && clearTimeout(sendPingTimeout);
|
|
165
|
-
sendPingTimeout = setTimeout(function() {
|
|
166
|
-
ping();
|
|
167
|
-
}, connectionCheckTimeout);
|
|
168
|
-
|
|
169
186
|
try {
|
|
170
187
|
if (params.content) {
|
|
171
188
|
data.content = JSON.stringify(params.content);
|
|
@@ -198,10 +215,9 @@
|
|
|
198
215
|
}
|
|
199
216
|
|
|
200
217
|
this.close = function() {
|
|
201
|
-
|
|
202
|
-
socketCloseTimeout && clearTimeout(socketCloseTimeout);
|
|
203
|
-
forceCloseTimeout && clearTimeout(forceCloseTimeout);
|
|
218
|
+
logLevel.debug && console.debug("[Async][Socket.js] Closing socket by call to this.close");
|
|
204
219
|
socket.close();
|
|
220
|
+
socketWatchTimeout && clearTimeout(socketWatchTimeout);
|
|
205
221
|
}
|
|
206
222
|
|
|
207
223
|
init();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function LogLevel(logLevel){
|
|
2
|
+
let ll = logLevel || 2;
|
|
3
|
+
switch (ll) {
|
|
4
|
+
case 1:
|
|
5
|
+
return {
|
|
6
|
+
error: true,
|
|
7
|
+
debug: false,
|
|
8
|
+
info: false,
|
|
9
|
+
}
|
|
10
|
+
case 2:
|
|
11
|
+
return {
|
|
12
|
+
error: true,
|
|
13
|
+
debug: true,
|
|
14
|
+
info: false,
|
|
15
|
+
}
|
|
16
|
+
case 3:
|
|
17
|
+
return {
|
|
18
|
+
error: true,
|
|
19
|
+
debug: true,
|
|
20
|
+
info: true,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if (typeof module !== 'undefined' && typeof module.exports != 'undefined') {
|
|
27
|
+
module.exports = LogLevel;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
if (!window.POD) {
|
|
31
|
+
window.POD = {};
|
|
32
|
+
}
|
|
33
|
+
window.POD.LogLevel = LogLevel;
|
|
34
|
+
}
|