podasync-ws-only 2.7.9 → 2.7.10-snapshot.1

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 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.9",
3
+ "version": "2.7.10-snapshot.1",
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",
@@ -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,
@@ -122,7 +125,8 @@
122
125
  socketAddress: params.socketAddress,
123
126
  wsConnectionWaitTime: params.wsConnectionWaitTime,
124
127
  connectionCheckTimeout: params.connectionCheckTimeout,
125
- connectionCheckTimeoutThreshold: params.connectionCheckTimeoutThreshold
128
+ connectionCheckTimeoutThreshold: params.connectionCheckTimeoutThreshold,
129
+ logLevel: logLevel
126
130
  });
127
131
 
128
132
  checkIfSocketHasOpennedTimeoutId = setTimeout(function () {
@@ -356,9 +360,12 @@
356
360
 
357
361
  if (peerId !== undefined) {
358
362
  content.refresh = true;
363
+ content.renew = false;
364
+
359
365
  }
360
366
  else {
361
367
  content.renew = true;
368
+ content.refresh = false;
362
369
  }
363
370
 
364
371
  pushSendData({
@@ -22,12 +22,45 @@
22
22
  eventCallback = {},
23
23
  socket,
24
24
  waitForSocketToConnectTimeoutId,
25
- forceCloseSocket = false,
26
- forceCloseSocketTimeout,
27
25
  socketRealTimeStatusInterval,
28
- sendPingTimeout,
26
+ //sendPingTimeout,
29
27
  socketCloseTimeout,
30
- forceCloseTimeout;
28
+ forceCloseTimeout,
29
+ logLevel = params.logLevel,
30
+ pingController = new PingManager({waitTime: connectionCheckTimeout});
31
+
32
+
33
+ function PingManager(params) {
34
+ const config = {
35
+ normalWaitTime: params.waitTime,
36
+
37
+ lastRequestTimeoutId: null,
38
+ lastReceivedMessageTime: 0,
39
+ }
40
+
41
+ return {
42
+ setPingTimeout() {
43
+ config.lastRequestTimeoutId = setInterval(()=>{
44
+ ping();
45
+ //this.watchForPingResponse();
46
+ }, config.normalWaitTime)
47
+ },
48
+ resetPingLoop() {
49
+ this.updateLastReceivedMessageTime();
50
+ this.stopPingLoop();
51
+ this.setPingTimeout();
52
+ // this.retries = 0;
53
+ },
54
+ stopPingLoop(){
55
+ // config.retries = 0;
56
+ clearInterval(config.lastRequestTimeoutId);
57
+ // clearTimeout(config.responseTimeoutId)
58
+ },
59
+ updateLastReceivedMessageTime() {
60
+ config.lastReceivedMessageTime = new Date().getTime();
61
+ },
62
+ }
63
+ }
31
64
 
32
65
  /*******************************************************
33
66
  * P R I V A T E M E T H O D S *
@@ -59,59 +92,26 @@
59
92
 
60
93
  socket.onopen = function(event) {
61
94
  waitForSocketToConnect(function() {
95
+ pingController.resetPingLoop();
62
96
  eventCallback["open"]();
63
97
  });
64
98
  }
65
99
 
66
100
  socket.onmessage = function(event) {
101
+ pingController.resetPingLoop();
102
+
67
103
  var messageData = JSON.parse(event.data);
68
104
  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
105
  }
109
106
 
110
107
  socket.onclose = function(event) {
108
+ pingController.stopPingLoop();
109
+ logLevel.debug && console.debug("[Async][Socket.js] socket.onclose happened. EventData:", event);
111
110
  onCloseHandler(event);
112
111
  }
113
112
 
114
113
  socket.onerror = function(event) {
114
+ logLevel.debug && console.debug("[Async][Socket.js] socket.onerror happened. EventData:", event);
115
115
  eventCallback["error"](event);
116
116
  }
117
117
  } catch (error) {
@@ -124,9 +124,7 @@
124
124
  },
125
125
 
126
126
  onCloseHandler = function(event) {
127
- sendPingTimeout && clearTimeout(sendPingTimeout);
128
- socketCloseTimeout && clearTimeout(socketCloseTimeout);
129
- forceCloseTimeout && clearTimeout(forceCloseTimeout);
127
+ pingController.stopPingLoop();
130
128
  eventCallback["close"](event);
131
129
  },
132
130
 
@@ -161,11 +159,6 @@
161
159
  data.trackerId = params.trackerId;
162
160
  }
163
161
 
164
- sendPingTimeout && clearTimeout(sendPingTimeout);
165
- sendPingTimeout = setTimeout(function() {
166
- ping();
167
- }, connectionCheckTimeout);
168
-
169
162
  try {
170
163
  if (params.content) {
171
164
  data.content = JSON.stringify(params.content);
@@ -198,9 +191,7 @@
198
191
  }
199
192
 
200
193
  this.close = function() {
201
- sendPingTimeout && clearTimeout(sendPingTimeout);
202
- socketCloseTimeout && clearTimeout(socketCloseTimeout);
203
- forceCloseTimeout && clearTimeout(forceCloseTimeout);
194
+ logLevel.debug && console.debug("[Async][Socket.js] Closing socket by call to this.close");
204
195
  socket.close();
205
196
  }
206
197
 
@@ -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
+ }