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

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.3",
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,50 @@
22
22
  eventCallback = {},
23
23
  socket,
24
24
  waitForSocketToConnectTimeoutId,
25
- forceCloseSocket = false,
26
- forceCloseSocketTimeout,
27
25
  socketRealTimeStatusInterval,
28
- sendPingTimeout,
29
- socketCloseTimeout,
30
- forceCloseTimeout;
26
+ logLevel = params.logLevel,
27
+ pingController = new PingManager({waitTime: connectionCheckTimeout});
28
+
29
+
30
+ function PingManager(params) {
31
+ const config = {
32
+ normalWaitTime: params.waitTime,
33
+
34
+ lastRequestTimeoutId: null,
35
+ lastReceivedMessageTime: 0,
36
+ totalNoMessageCount: 0,
37
+ timeoutIds: {
38
+ first: null,
39
+ second: null,
40
+ third: null,
41
+ fourth: null
42
+ }
43
+ }
44
+
45
+ return {
46
+ resetPingLoop() {
47
+ this.stopPingLoop();
48
+ this.setPingTimeout();
49
+ },
50
+ setPingTimeout() {
51
+ config.timeoutIds.first = setTimeout(()=>{
52
+ ping();
53
+ config.timeoutIds.first = setTimeout(()=>{
54
+ ping();
55
+ config.timeoutIds.fourth = setTimeout(()=>{
56
+ socket.close();
57
+ }, 2000);
58
+ }, 2000);
59
+ }, 8000);
60
+ },
61
+ stopPingLoop(){
62
+ clearTimeout(config.timeoutIds.first);
63
+ clearTimeout(config.timeoutIds.second);
64
+ clearTimeout(config.timeoutIds.third);
65
+ clearTimeout(config.timeoutIds.fourth);
66
+ },
67
+ }
68
+ }
31
69
 
32
70
  /*******************************************************
33
71
  * P R I V A T E M E T H O D S *
@@ -59,59 +97,26 @@
59
97
 
60
98
  socket.onopen = function(event) {
61
99
  waitForSocketToConnect(function() {
100
+ pingController.resetPingLoop();
62
101
  eventCallback["open"]();
63
102
  });
64
103
  }
65
104
 
66
105
  socket.onmessage = function(event) {
106
+ pingController.resetPingLoop();
107
+
67
108
  var messageData = JSON.parse(event.data);
68
109
  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
110
  }
109
111
 
110
112
  socket.onclose = function(event) {
113
+ pingController.stopPingLoop();
114
+ logLevel.debug && console.debug("[Async][Socket.js] socket.onclose happened. EventData:", event);
111
115
  onCloseHandler(event);
112
116
  }
113
117
 
114
118
  socket.onerror = function(event) {
119
+ logLevel.debug && console.debug("[Async][Socket.js] socket.onerror happened. EventData:", event);
115
120
  eventCallback["error"](event);
116
121
  }
117
122
  } catch (error) {
@@ -124,9 +129,7 @@
124
129
  },
125
130
 
126
131
  onCloseHandler = function(event) {
127
- sendPingTimeout && clearTimeout(sendPingTimeout);
128
- socketCloseTimeout && clearTimeout(socketCloseTimeout);
129
- forceCloseTimeout && clearTimeout(forceCloseTimeout);
132
+ pingController.stopPingLoop();
130
133
  eventCallback["close"](event);
131
134
  },
132
135
 
@@ -161,11 +164,6 @@
161
164
  data.trackerId = params.trackerId;
162
165
  }
163
166
 
164
- sendPingTimeout && clearTimeout(sendPingTimeout);
165
- sendPingTimeout = setTimeout(function() {
166
- ping();
167
- }, connectionCheckTimeout);
168
-
169
167
  try {
170
168
  if (params.content) {
171
169
  data.content = JSON.stringify(params.content);
@@ -198,9 +196,7 @@
198
196
  }
199
197
 
200
198
  this.close = function() {
201
- sendPingTimeout && clearTimeout(sendPingTimeout);
202
- socketCloseTimeout && clearTimeout(socketCloseTimeout);
203
- forceCloseTimeout && clearTimeout(forceCloseTimeout);
199
+ logLevel.debug && console.debug("[Async][Socket.js] Closing socket by call to this.close");
204
200
  socket.close();
205
201
  }
206
202
 
@@ -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
+ }