@symbo.ls/socket 2.6.6 → 2.6.8
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/client.js +36 -15
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -1,29 +1,34 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
import { isFunction } from '@domql/utils'
|
|
3
|
+
import { isFunction, isArray } from '@domql/utils'
|
|
4
4
|
import io from 'socket.io-client'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
// const SOCKET_BACKEND_URL = 'ws://52.14.150.69:8080'
|
|
8
|
-
// const SOCKET_BACKEND_URL = 'ws://ec2-52-14-150-69.us-east-2.compute.amazonaws.com:8080'
|
|
9
|
-
// const SOCKET_BACKEND_URL = 'ws://ec2-52-14-150-69.us-east-2.compute.amazonaws.com:8080'
|
|
10
|
-
// const SOCKET_BACKEND_URL = 'ws://52.14.150.69:8080'
|
|
6
|
+
const ENV = process.env.NODE_ENV
|
|
11
7
|
|
|
12
8
|
const SOCKET_BACKEND_URL = window.location
|
|
13
|
-
.host.includes('
|
|
14
|
-
? '
|
|
15
|
-
: '
|
|
9
|
+
.host.includes('local')
|
|
10
|
+
? 'localhost:13335'
|
|
11
|
+
: 'socket.symbols.app'
|
|
16
12
|
|
|
17
13
|
let socket
|
|
18
14
|
const defautlOpts = {}
|
|
19
15
|
|
|
20
16
|
export const connect = (key, options = {}) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
const socketUrls = isArray(options.socketUrl) ? options.socketUrl : [options.socketUrl || SOCKET_BACKEND_URL]
|
|
18
|
+
const primaryUrl = socketUrls[0]
|
|
19
|
+
const secondaryUrl = socketUrls[1] || 'socket.symbols.app'
|
|
20
|
+
|
|
21
|
+
socket = io(primaryUrl || SOCKET_BACKEND_URL)
|
|
24
22
|
|
|
25
23
|
socket.on('connect', () => {
|
|
26
|
-
|
|
24
|
+
if (ENV === 'test' || ENV === 'development') {
|
|
25
|
+
console.log(
|
|
26
|
+
`Connected to %c${primaryUrl || SOCKET_BACKEND_URL} %c${key} %c${socket.id}`,
|
|
27
|
+
'font-weight: bold; color: green;',
|
|
28
|
+
'font-weight: bold;',
|
|
29
|
+
''
|
|
30
|
+
)
|
|
31
|
+
}
|
|
27
32
|
|
|
28
33
|
socket.emit('initConnect', options)
|
|
29
34
|
|
|
@@ -34,13 +39,29 @@ export const connect = (key, options = {}) => {
|
|
|
34
39
|
}
|
|
35
40
|
})
|
|
36
41
|
|
|
42
|
+
let tryConnect = 0
|
|
37
43
|
socket.on('connect_error', (err) => {
|
|
38
44
|
console.log(`event: connect_error | reason: ${err.message}`)
|
|
39
45
|
try {
|
|
40
46
|
if (isFunction(options.onError)) options.onError(err, socket)
|
|
47
|
+
if (tryConnect === 2) {
|
|
48
|
+
socket.disconnect()
|
|
49
|
+
|
|
50
|
+
if (ENV === 'test' || ENV === 'development') {
|
|
51
|
+
console.log(
|
|
52
|
+
'Could not connect to %c' + primaryUrl + '%c, reconnecting to %c' + secondaryUrl,
|
|
53
|
+
'font-weight: bold; color: red;',
|
|
54
|
+
'',
|
|
55
|
+
'font-weight: bold; color: green;'
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
connect(key, { ...options, socketUrl: secondaryUrl })
|
|
60
|
+
}
|
|
41
61
|
} catch (e) {
|
|
42
62
|
console.error(e)
|
|
43
63
|
}
|
|
64
|
+
tryConnect++
|
|
44
65
|
})
|
|
45
66
|
|
|
46
67
|
socket.on('disconnect', (reason) => {
|
|
@@ -67,6 +88,6 @@ export const send = (event = 'change', changes, options) => {
|
|
|
67
88
|
socket.emit(event, changes, { ...options, ...defautlOpts })
|
|
68
89
|
}
|
|
69
90
|
|
|
70
|
-
export const disconnect =
|
|
71
|
-
socket.
|
|
91
|
+
export const disconnect = () => {
|
|
92
|
+
socket.disconnect()
|
|
72
93
|
}
|