-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsock.js
46 lines (41 loc) · 1.04 KB
/
sock.js
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
import Stomp from 'stompjs'
import SockJS from 'sockjs-client'
var stompClient = null
function initConnection() {
var socket = new SockJS(process.env.WS_URL + '/websocket')
stompClient = Stomp.over(socket)
}
var Subscribe = {
isConnected: function() {
console.log(stompClient)
if (stompClient === null || stompClient.connected === false) {
return false
} else {
return true
}
},
listen: function(destination, callback) {
if (stompClient === null) {
initConnection()
}
stompClient.connect({}, (frame) => {
console.log('Connected: ' + frame)
stompClient.subscribe(destination, callback)
})
},
send: function(destination, data) {
if (stompClient === null) {
console.log('WebSocket通信已关闭,请刷新页面重新连接')
} else {
stompClient.send(destination, {}, data)
}
},
disconnect: function() {
if (stompClient !== null) {
stompClient.disconnect()
stompClient = null
}
console.log('Disconnected')
}
}
export default Subscribe