|
@@ -1,19 +1,19 @@
|
|
|
class websocketUtil {
|
|
|
- var is_open_socket = false;
|
|
|
- var socketStatus = 0 //避免重复连接 0 无连接 1-9连接中 10 已连接
|
|
|
- var url = '';
|
|
|
- var data = '';
|
|
|
- var uuid = '';
|
|
|
- var timeout = 10000;
|
|
|
- var heartbeatInterval = null;
|
|
|
- var reconnectTimeOut = null;
|
|
|
- var reconnectNum = 0 //重连次数
|
|
|
- var msgQueue = []
|
|
|
- var socketTask = null //ws对象
|
|
|
- var netIsConnected = false
|
|
|
- var netType = 'none'
|
|
|
+ is_open_socket = false;
|
|
|
+ socketStatus = 0 //避免重复连接 0 无连接 1-9连接中 10 已连接
|
|
|
+ url = '';
|
|
|
+ data = '';
|
|
|
+ uuid = '';
|
|
|
+ timeout = 10000;
|
|
|
+ heartbeatInterval = null;
|
|
|
+ reconnectTimeOut = null;
|
|
|
+ reconnectNum = 0 //重连次数
|
|
|
+ msgQueue = []
|
|
|
+ socketTask = null //ws对象
|
|
|
+ netIsConnected = false
|
|
|
+ netType = 'none'
|
|
|
|
|
|
- var autoFlag = true
|
|
|
+ autoFlag = true
|
|
|
|
|
|
|
|
|
constructor(url, uuid) {
|
|
@@ -59,6 +59,50 @@ class websocketUtil {
|
|
|
// this.reconnect();
|
|
|
// }
|
|
|
}
|
|
|
+ //重新连接
|
|
|
+ reconnect() {
|
|
|
+ // 失败重连频率
|
|
|
+ const reconnect_time = [3000, 3000, 3000, 3000, 10000, 10000, 10000, 20000, 20000, 50000]
|
|
|
+ // 自身巡检频率
|
|
|
+ let reconnect_timeout = 30000
|
|
|
+ //停止发送心跳
|
|
|
+ this.heartbeatInterval && clearInterval(this.heartbeatInterval)
|
|
|
+ if (this.socketStatus < 10) {
|
|
|
+ // 失败重连
|
|
|
+ if (this.reconnectNum > 50) return;
|
|
|
+ if (this.reconnectNum < this.reconnect_time.length) {
|
|
|
+ reconnect_timeout = reconnect_time[this.reconnectNum]
|
|
|
+ } else {
|
|
|
+ reconnect_timeout = reconnect_time[this.reconnect_time.length - 1]
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log("uniWebsocket 重新连接", this.socketStatus, this.reconnectNum, this.reconnect_timeout)
|
|
|
+
|
|
|
+ this.reconnectNum = this.reconnectNum + 1
|
|
|
+
|
|
|
+ this.reconnectTimeOut = setTimeout(() => {
|
|
|
+ // 连接创建
|
|
|
+ this.connectSocketInit();
|
|
|
+ this.reconnect()
|
|
|
+ }, reconnect_timeout)
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ console.log("uniWebsocket 自身巡检", socketStatus, reconnectNum, reconnect_timeout)
|
|
|
+
|
|
|
+ // 自身巡检循环
|
|
|
+ this.reconnectTimeOut = setTimeout(() => {
|
|
|
+ // 连接创建
|
|
|
+ this.connectSocketInit();
|
|
|
+ this.reconnect()
|
|
|
+ }, reconnect_timeout)
|
|
|
+ //如果不是人为关闭的话,进行重连
|
|
|
+ // if (!this.is_open_socket) {
|
|
|
+ // this.reconnectTimeOut = setTimeout(() => {
|
|
|
+ // this.connectSocketInit();
|
|
|
+ // }, 3000)
|
|
|
+ // }
|
|
|
+ }
|
|
|
generateSeq() {
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
|
const r = Math.random() * 16 | 0;
|
|
@@ -197,17 +241,6 @@ class websocketUtil {
|
|
|
stop() {
|
|
|
uni.closeSocket();
|
|
|
}
|
|
|
- //重新连接
|
|
|
- reconnect() {
|
|
|
- //停止发送心跳
|
|
|
- clearInterval(this.heartbeatInterval)
|
|
|
- //如果不是人为关闭的话,进行重连
|
|
|
- if (!this.is_open_socket) {
|
|
|
- this.reconnectTimeOut = setTimeout(() => {
|
|
|
- this.connectSocketInit();
|
|
|
- }, 3000)
|
|
|
- }
|
|
|
- }
|
|
|
//外部获取消息
|
|
|
getMessage(callback) {
|
|
|
this.socketTask.onMessage((res) => {
|