-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
95 lines (91 loc) · 2.32 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="close">关闭</button>
<button id="open">开启</button>
<script src="./dist/one-socket.js"></script>
<script>
let WS
let timer
function connect() {
const protocolMap = {
'http:': 'ws',
'https:': 'wss'
}
let url = ''
if (window.__POWERED_BY_QIANKUN__) {
// eslint-disable-next-line no-undef,camelcase
url = __webpack_public_path__.split('//')[1]
} else {
url = window.location.host + '/'
}
const config = {
url: `${protocolMap[window.location.protocol]}://192.168.69.232/ws/home/overview`,
hasHeartbeat: true,
heartbeatInterval: 50000,
responseParser: responseParser,
onClose: function () {
console.log('----')
clearInterval(timer)
}
}
const INSTANCE = new OneSocket(config)
.then((ws) => {
WS = ws
const params = {
lang: 'zh_CN',
service: 'connect',
token: ''
}
ws.send('connect', params)
.then((res) => {
localStorage.setItem('token', JSON.stringify({ token: res.token }))
ws.updateConfig({
heartbeatPack: {
lang: 'zh_cn',
service: 'ping',
token: res.token
}
})
})
.catch((err) => {
console.log(err)
})
/*timer = setInterval(() => {
ws.send('state', {
lang: 'zh_CN',
service: 'state',
token: ''
} )
}, 5000)
ws.on('state', (data) => {
console.log(data)
})*/
})
window.INSTANCE = INSTANCE
}
function responseParser (res, next, scope) {
const json = JSON.parse(res.data)
let data = json.result_data || {}
const service = data.service || 'ping'
const ResultCode = json.result_code
data = Object.assign({}, data, {
message: json.result_msg,
code: json.result_code
})
next(data, { service }, ResultCode === 1, scope)
}
connect()
document.getElementById('close').addEventListener('click', function () {
WS.close()
})
document.getElementById('open').addEventListener('click', function () {
connect()
})
</script>
</body>
</html>