@knowlearning/agents 0.3.16 → 0.3.17
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/agents/generic.js +18 -0
- package/agents/node.js +1 -1
- package/package.json +1 -1
package/agents/generic.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import MutableProxy from '../persistence/json.js'
|
|
2
2
|
|
|
3
|
+
const HEARTBEAT_TIMEOUT = 10000
|
|
4
|
+
|
|
3
5
|
// transform our custom path implementation to the standard JSONPatch path
|
|
4
6
|
function standardJSONPatch(patch) {
|
|
5
7
|
return patch.map(p => {
|
|
@@ -78,16 +80,32 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
78
80
|
flushMessageQueue()
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
let lastHeartbeat
|
|
84
|
+
function checkHeartbeat() {
|
|
85
|
+
clearTimeout(lastHeartbeat)
|
|
86
|
+
lastHeartbeat = setTimeout(
|
|
87
|
+
() => {
|
|
88
|
+
log('CLOSING DUE TO HEARTBEAT TIMEOUT')
|
|
89
|
+
ws.close()
|
|
90
|
+
},
|
|
91
|
+
HEARTBEAT_TIMEOUT
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
81
95
|
function initWS() {
|
|
82
96
|
ws = new WebSocket(`${protocol}://${host}`)
|
|
83
97
|
|
|
84
98
|
ws.onopen = async () => {
|
|
99
|
+
checkHeartbeat()
|
|
85
100
|
log('AUTHORIZING NEWLY OPENED WS FOR SESSION:', session)
|
|
86
101
|
failedConnections = 0
|
|
87
102
|
ws.send(JSON.stringify({ token: await token(), session }))
|
|
88
103
|
}
|
|
89
104
|
|
|
90
105
|
ws.onmessage = async ({ data }) => {
|
|
106
|
+
checkHeartbeat()
|
|
107
|
+
if (data.length === 0) return // heartbeat
|
|
108
|
+
|
|
91
109
|
try {
|
|
92
110
|
log('handling message', disconnected, authed)
|
|
93
111
|
const message = JSON.parse(data)
|
package/agents/node.js
CHANGED