forked from xiaoai7904/vue_admin_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsocket.module.js
More file actions
124 lines (115 loc) · 2.99 KB
/
Websocket.module.js
File metadata and controls
124 lines (115 loc) · 2.99 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable no-empty-function */
import Vue from 'vue';
import SockJS from 'sockjs-client';
import Utils from '@/module/utils/Utils.module.js';
import TimerManager from '@/module/timerManager/TimerManager.module.js';
const Stomp = require('stompjs');
let try2ConnectCount = 5;
let webscoketIns = null
class WebScoket {
isConnectSuccess = false;
constructor() {
if (webscoketIns) {
return webscoketIns
}
this.vuePro = Vue.prototype;
webscoketIns = this
}
/**
* 设置连接锁
*/
setConnectLock(value) {
this.isConnectSuccess = value
}
/**
* 初始化websocket
*/
initWebSocket(params, url = '', subscribes = ['subscribeSendNotice']) {
this.params = params;
this.subscribes = subscribes;
this.handelrUrl(url);
!this.isConnectSuccess && this.connection();
}
/**
* 处理websocket地址
*/
handelrUrl(url) {
if (url) {
this.url = url;
} else {
// window.SOCKET_URL = ''
url = window.SOCKET_URL ? window.SOCKET_URL : 'http://192.168.1.188:10000';
let socketUrl = Utils.of().getSplitUrl(url);
url = socketUrl.prefix + socketUrl.suffix.replace(/\/+/g, '');
this.url = `${url}/live-websocket?username=${this.params.username}`;
}
}
/**
* 建立链接
*/
connection() {
try {
// 连接服务端提供的通信接口,连接以后才可以订阅广播消息和个人消息
this.socket = new SockJS(this.url);
// 获取STOMP子协议的客户端对象
this.stompClient = Stomp.over(this.socket);
this.stompClient.debug = () => {};
// 向服务器发起链接
this.connectServer();
this.isConnectSuccess = true;
} catch (error) {
console.error(error);
}
}
/**
* 连接服务
*/
connectServer() {
this.stompClient.connect(
{},
() => {
console.log('websocket连接成功');
TimerManager.of().stopSetTimeout('websocket');
this.subscribes.forEach(item => {
typeof this[item] === 'function' && this[item]();
});
},
err => {
console.error(err);
if (try2ConnectCount <= 0) {
TimerManager.of().stopSetTimeout('websocket');
} else {
TimerManager.of()
.addSetTimeout('websocket', () => {
console.log('连接中断,尝试重新连接...');
this.connection();
try2ConnectCount--;
})
.startSetTimout('websocket', 3 * 1000);
}
}
);
}
subscribeSendNotice() {
try {
this.stompClient.subscribe('/topic/sendNotice', msg => {
this.vuePro.$customEvent.trigger('update_announcement', true)
});
} catch (error) {
console.error(error);
}
}
/**
* 断开链接
*/
disconnect() {
if (this.stompClient != null) {
this.stompClient.disconnect();
TimerManager.of().stopSetTimeout('websocket');
}
}
}
WebScoket.of = function () {
return new WebScoket();
};
export default WebScoket;