@jcbuisson/express-x-client 1.6.2 → 2.0.0
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/package.json +1 -1
- package/src/index.mjs +35 -118
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,102 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
export default function expressXClient(socket, options={}) {
|
|
3
|
-
if (options.debug === undefined) options.debug = false
|
|
4
|
-
if (options.timeout === undefined) options.timeout = 5000
|
|
5
|
-
|
|
6
|
-
const waitingPromisesByUid = {}
|
|
7
|
-
const action2service2handlers = {}
|
|
8
|
-
const type2appHandlers = {}
|
|
9
|
-
let onConnectionCallback = null
|
|
10
|
-
let onReconnectionCallback = null
|
|
11
|
-
let onDisconnectionCallback = null
|
|
12
|
-
let nodeCnxId
|
|
13
|
-
|
|
14
|
-
const setConnectionCallback = (callback) => {
|
|
15
|
-
onConnectionCallback = callback
|
|
16
|
-
}
|
|
17
1
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
function generateUID(length) {
|
|
3
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
4
|
+
let uid = '';
|
|
21
5
|
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
for (let i = 0; i < length; i++) {
|
|
7
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
8
|
+
uid += characters.charAt(randomIndex)
|
|
24
9
|
}
|
|
10
|
+
return uid
|
|
11
|
+
}
|
|
25
12
|
|
|
26
|
-
function _getCnxId() {
|
|
27
|
-
if (typeof sessionStorage !== 'undefined') return sessionStorage.getItem('expressx-cnx-id')
|
|
28
|
-
return nodeCnxId
|
|
29
|
-
}
|
|
30
13
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
} else {
|
|
35
|
-
nodeCnxId = id
|
|
36
|
-
}
|
|
37
|
-
}
|
|
14
|
+
export default function expressXClient(socket, options={}) {
|
|
15
|
+
if (options.debug === undefined) options.debug = false
|
|
16
|
+
if (options.timeout === undefined) options.timeout = 5000
|
|
38
17
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// look for a previously stored connection id
|
|
43
|
-
const prevConnectionId = _getCnxId()
|
|
44
|
-
if (prevConnectionId) {
|
|
45
|
-
// it's a reconnection
|
|
46
|
-
if (prevConnectionId < 0) {
|
|
47
|
-
// ask server to transfer all data from connection `prevConnectionId` to connection `connectionId`
|
|
48
|
-
if (options.debug) console.log('cnx-transfer', -prevConnectionId, 'to', connectionId)
|
|
49
|
-
socket.emit('cnx-transfer', {
|
|
50
|
-
from: -prevConnectionId,
|
|
51
|
-
to: connectionId,
|
|
52
|
-
})
|
|
53
|
-
// set/update connection id
|
|
54
|
-
_setCnxId(connectionId)
|
|
55
|
-
} else {
|
|
56
|
-
if (options.debug) console.log('Error, previous connection id should be negative', prevConnectionId)
|
|
57
|
-
}
|
|
18
|
+
const waitingPromisesByUid = {}
|
|
19
|
+
const action2service2handlers = {}
|
|
20
|
+
const socketConnectionState = {}
|
|
58
21
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
22
|
+
socket.on("connect", async () => {
|
|
23
|
+
console.log("socket connected", socket.id)
|
|
24
|
+
if (socketConnectionState.resolve) {
|
|
25
|
+
socketConnectionState.resolve('ok')
|
|
26
|
+
socketConnectionState.status = 'connected'
|
|
62
27
|
}
|
|
63
|
-
// call user-defined connection callback
|
|
64
|
-
if (onConnectionCallback) onConnectionCallback(connectionId)
|
|
65
28
|
})
|
|
66
29
|
|
|
67
|
-
socket.on("
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
// A negative value for the connexion id means that the connection with the server has been lost
|
|
76
|
-
// Requests must wait until it goes positive again
|
|
77
|
-
|
|
78
|
-
// disconnection due to network issues
|
|
79
|
-
socket.on("disconnect", async (cause) => {
|
|
80
|
-
const id = _getCnxId()
|
|
81
|
-
if (id > 0) {
|
|
82
|
-
_setCnxId(-id)
|
|
83
|
-
} else {
|
|
84
|
-
if (options.debug) console.log('Error (disconnect), connection id should be negative', id)
|
|
30
|
+
socket.on("error", async () => {
|
|
31
|
+
console.log("socket connection error", socket.id)
|
|
32
|
+
if (socketConnectionState.reject) {
|
|
33
|
+
socketConnectionState.reject(err)
|
|
34
|
+
socketConnectionState.status = 'error'
|
|
85
35
|
}
|
|
86
36
|
})
|
|
87
37
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (id > 0) {
|
|
93
|
-
_setCnxId(-id)
|
|
94
|
-
} else {
|
|
95
|
-
if (options.debug) console.log('Error (unload), connection id should be negative', id)
|
|
96
|
-
}
|
|
38
|
+
async function socketConnection() {
|
|
39
|
+
const promise = new Promise((resolve, reject) => {
|
|
40
|
+
socketConnectionState.resolve = resolve
|
|
41
|
+
socketConnectionState.reject = reject
|
|
97
42
|
})
|
|
43
|
+
return promise
|
|
98
44
|
}
|
|
99
45
|
|
|
46
|
+
function socketStatus() {
|
|
47
|
+
return socketConnectionState.status
|
|
48
|
+
}
|
|
100
49
|
|
|
101
50
|
// on receiving response from service request
|
|
102
51
|
socket.on('client-response', ({ uid, error, result }) => {
|
|
@@ -127,21 +76,8 @@ export default function expressXClient(socket, options={}) {
|
|
|
127
76
|
const handler = type2appHandlers[type]
|
|
128
77
|
if (handler) handler(value)
|
|
129
78
|
})
|
|
130
|
-
|
|
131
|
-
function wait(ms) {
|
|
132
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
133
|
-
}
|
|
134
|
-
|
|
79
|
+
|
|
135
80
|
async function serviceMethodRequest(name, action, ...args) {
|
|
136
|
-
// wait while stored connexion id is negative (= connection is lost with server)
|
|
137
|
-
let retries = 10
|
|
138
|
-
while (retries-- > 0) {
|
|
139
|
-
const id = _getCnxId()
|
|
140
|
-
if (id > 0) break
|
|
141
|
-
await wait(200)
|
|
142
|
-
}
|
|
143
|
-
if (retries === 0) throw new Error(`Timeout waiting for reconnection`)
|
|
144
|
-
|
|
145
81
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
146
82
|
const uid = generateUID(20)
|
|
147
83
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -163,12 +99,6 @@ export default function expressXClient(socket, options={}) {
|
|
|
163
99
|
return promise
|
|
164
100
|
}
|
|
165
101
|
|
|
166
|
-
// define application events handlers
|
|
167
|
-
function on(type, handler) {
|
|
168
|
-
if (!type2appHandlers[type]) type2appHandlers[type] = {}
|
|
169
|
-
type2appHandlers[type] = handler
|
|
170
|
-
}
|
|
171
|
-
|
|
172
102
|
function service(name) {
|
|
173
103
|
const service = {
|
|
174
104
|
// associate a handler to a pub/sub event for this service
|
|
@@ -192,22 +122,9 @@ export default function expressXClient(socket, options={}) {
|
|
|
192
122
|
}
|
|
193
123
|
|
|
194
124
|
return {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
setDisconnectionCallback,
|
|
125
|
+
socketConnection,
|
|
126
|
+
socketStatus,
|
|
198
127
|
service,
|
|
199
|
-
on,
|
|
200
128
|
}
|
|
201
129
|
}
|
|
202
130
|
|
|
203
|
-
|
|
204
|
-
function generateUID(length) {
|
|
205
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
206
|
-
let uid = '';
|
|
207
|
-
|
|
208
|
-
for (let i = 0; i < length; i++) {
|
|
209
|
-
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
210
|
-
uid += characters.charAt(randomIndex)
|
|
211
|
-
}
|
|
212
|
-
return uid
|
|
213
|
-
}
|