浏览代码

Merge branch 'develop' into lmm

seimin 3 年之前
父节点
当前提交
267b72cf64

+ 24 - 15
src/app/services/websocket-index.service.ts

@@ -6,29 +6,32 @@ import * as Atmosphere from "atmosphere.js";
6
   providedIn: "root",
6
   providedIn: "root",
7
 })
7
 })
8
 export class WebsocketIndexService {
8
 export class WebsocketIndexService {
9
-  constructor() {}
9
+  constructor() { }
10
   private lockReconnect = false; //避免ws重复连接
10
   private lockReconnect = false; //避免ws重复连接
11
-  ws; //定义websocket
11
+  ws = null; //定义websocket
12
   private url = ""; //ws连接地址
12
   private url = ""; //ws连接地址
13
   private urlParams; //ws连接传参
13
   private urlParams; //ws连接传参
14
   private isHandler = false; //是否手动
14
   private isHandler = false; //是否手动
15
   private subject;
15
   private subject;
16
+  private reconnectTimer = null;//重连的计时器
16
   // 连接websocket
17
   // 连接websocket
17
   connectWs(url, data): Observable<any> {
18
   connectWs(url, data): Observable<any> {
18
     this.url = url;
19
     this.url = url;
19
     this.urlParams = data;
20
     this.urlParams = data;
20
     this.subject = new Subject<any>();
21
     this.subject = new Subject<any>();
21
     let request: any = {
22
     let request: any = {
22
-      url,
23
-      contentType: "application/json",
24
-      shared: true, // 标签共享
23
+      webSocketUrl: url,
25
       trackMessageLength: true, //校验数据完整性
24
       trackMessageLength: true, //校验数据完整性
26
       transport: "websocket",
25
       transport: "websocket",
26
+      handleOnlineOffline: false,
27
+      suspend: false,
28
+      maxReconnectOnClose: 0,
27
     };
29
     };
28
 
30
 
29
     request.onOpen = (response) => {
31
     request.onOpen = (response) => {
30
       console.log("ws连接成功" + new Date().toLocaleString());
32
       console.log("ws连接成功" + new Date().toLocaleString());
31
       this.heartCheck.reset().start(this);
33
       this.heartCheck.reset().start(this);
34
+      console.log(response);
32
       this.ws.push(JSON.stringify(data));
35
       this.ws.push(JSON.stringify(data));
33
     };
36
     };
34
 
37
 
@@ -36,7 +39,6 @@ export class WebsocketIndexService {
36
       this.heartCheck.reset().start(this);
39
       this.heartCheck.reset().start(this);
37
       let message = response.responseBody;
40
       let message = response.responseBody;
38
       try {
41
       try {
39
-        // alert(message);
40
         console.log("收到消息" + message);
42
         console.log("收到消息" + message);
41
         if (message !== "pong") {
43
         if (message !== "pong") {
42
           this.subject.next(JSON.parse(message));
44
           this.subject.next(JSON.parse(message));
@@ -49,20 +51,20 @@ export class WebsocketIndexService {
49
 
51
 
50
     request.onClose = (response) => {
52
     request.onClose = (response) => {
51
       console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
53
       console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
52
-      this.heartCheck.reset();
53
-      if (!this.isHandler) {
54
+      this.ws = null;
55
+      console.log(response)
56
+      if (!this.isHandler && response.status === 408) {
54
         this.reconnect(this.url, this.urlParams);
57
         this.reconnect(this.url, this.urlParams);
55
       }
58
       }
56
     };
59
     };
57
 
60
 
58
     request.onError = (response) => {
61
     request.onError = (response) => {
59
       console.log("ws连接错误", this.isHandler);
62
       console.log("ws连接错误", this.isHandler);
60
-      this.heartCheck.reset();
61
-      if (!this.isHandler) {
63
+      this.ws = null;
64
+      if (response.status === 0) {
62
         this.reconnect(this.url, this.urlParams);
65
         this.reconnect(this.url, this.urlParams);
63
       }
66
       }
64
     };
67
     };
65
-
66
     this.ws = Atmosphere.subscribe(request);
68
     this.ws = Atmosphere.subscribe(request);
67
     return this.subject.asObservable();
69
     return this.subject.asObservable();
68
   }
70
   }
@@ -70,14 +72,14 @@ export class WebsocketIndexService {
70
   private reconnect(url, data) {
72
   private reconnect(url, data) {
71
     if (this.lockReconnect) return;
73
     if (this.lockReconnect) return;
72
     this.lockReconnect = true;
74
     this.lockReconnect = true;
73
-    setTimeout(() => {
75
+    this.reconnectTimer = setTimeout(() => {
74
       //没连接上会一直重连,设置延迟避免请求过多
76
       //没连接上会一直重连,设置延迟避免请求过多
75
       this.connectWs(url, data);
77
       this.connectWs(url, data);
76
       this.lockReconnect = false;
78
       this.lockReconnect = false;
77
     }, 15000);
79
     }, 15000);
78
   }
80
   }
79
   private heartCheck = {
81
   private heartCheck = {
80
-    timeout: 60000, //1分钟发一次心跳
82
+    timeout: 30000, //发一次心跳的间隔时间
81
     timeoutObj: null,
83
     timeoutObj: null,
82
     serverTimeoutObj: null,
84
     serverTimeoutObj: null,
83
     reset: function () {
85
     reset: function () {
@@ -89,7 +91,9 @@ export class WebsocketIndexService {
89
       this.timeoutObj = setTimeout(() => {
91
       this.timeoutObj = setTimeout(() => {
90
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
92
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
         //onmessage拿到返回的心跳就说明连接正常
93
         //onmessage拿到返回的心跳就说明连接正常
92
-        _this.ws.push("ping");
94
+        if (_this.ws) {
95
+          _this.ws.push("ping");
96
+        }
93
         this.serverTimeoutObj = setTimeout(() => {
97
         this.serverTimeoutObj = setTimeout(() => {
94
           //如果超过一定时间还没重置,说明后端主动断开了
98
           //如果超过一定时间还没重置,说明后端主动断开了
95
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
99
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
@@ -101,6 +105,11 @@ export class WebsocketIndexService {
101
   // 断开websocket
105
   // 断开websocket
102
   closeWs(flag: boolean = false) {
106
   closeWs(flag: boolean = false) {
103
     this.isHandler = flag;
107
     this.isHandler = flag;
104
-    this.ws.close();
108
+    if (flag) {
109
+      clearTimeout(this.reconnectTimer);
110
+    }
111
+    if (this.ws) {
112
+      this.ws.close();
113
+    }
105
   }
114
   }
106
 }
115
 }

+ 26 - 17
src/app/services/websocket-main.service.ts

@@ -6,29 +6,32 @@ import * as Atmosphere from "atmosphere.js";
6
   providedIn: "root",
6
   providedIn: "root",
7
 })
7
 })
8
 export class WebsocketMainService {
8
 export class WebsocketMainService {
9
-  constructor() {}
9
+  constructor() { }
10
   private lockReconnect = false; //避免ws重复连接
10
   private lockReconnect = false; //避免ws重复连接
11
-  ws; //定义websocket
11
+  ws = null; //定义websocket
12
   private url = ""; //ws连接地址
12
   private url = ""; //ws连接地址
13
   private urlParams; //ws连接传参
13
   private urlParams; //ws连接传参
14
   private isHandler = false; //是否手动
14
   private isHandler = false; //是否手动
15
   private subject;
15
   private subject;
16
+  private reconnectTimer = null;//重连的计时器
16
   // 连接websocket
17
   // 连接websocket
17
   connectWs(url, data): Observable<any> {
18
   connectWs(url, data): Observable<any> {
18
     this.url = url;
19
     this.url = url;
19
     this.urlParams = data;
20
     this.urlParams = data;
20
     this.subject = new Subject<any>();
21
     this.subject = new Subject<any>();
21
     let request: any = {
22
     let request: any = {
22
-      url,
23
-      contentType: "application/json",
24
-      shared: true, // 标签共享
23
+      webSocketUrl: url,
25
       trackMessageLength: true, //校验数据完整性
24
       trackMessageLength: true, //校验数据完整性
26
       transport: "websocket",
25
       transport: "websocket",
26
+      handleOnlineOffline: false,
27
+      suspend: false,
28
+      maxReconnectOnClose: 0,
27
     };
29
     };
28
 
30
 
29
     request.onOpen = (response) => {
31
     request.onOpen = (response) => {
30
       console.log("ws连接成功" + new Date().toLocaleString());
32
       console.log("ws连接成功" + new Date().toLocaleString());
31
       this.heartCheck.reset().start(this);
33
       this.heartCheck.reset().start(this);
34
+      console.log(response);
32
       this.ws.push(JSON.stringify(data));
35
       this.ws.push(JSON.stringify(data));
33
     };
36
     };
34
 
37
 
@@ -36,7 +39,6 @@ export class WebsocketMainService {
36
       this.heartCheck.reset().start(this);
39
       this.heartCheck.reset().start(this);
37
       let message = response.responseBody;
40
       let message = response.responseBody;
38
       try {
41
       try {
39
-        // alert(message);
40
         console.log("收到消息" + message);
42
         console.log("收到消息" + message);
41
         if (message !== "pong") {
43
         if (message !== "pong") {
42
           this.subject.next(JSON.parse(message));
44
           this.subject.next(JSON.parse(message));
@@ -48,21 +50,21 @@ export class WebsocketMainService {
48
     };
50
     };
49
 
51
 
50
     request.onClose = (response) => {
52
     request.onClose = (response) => {
51
-      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
-      this.heartCheck.reset();
53
-      if (!this.isHandler) {
53
+      console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
54
+      this.ws = null;
55
+      console.log(response)
56
+      if (!this.isHandler && response.status === 408) {
54
         this.reconnect(this.url, this.urlParams);
57
         this.reconnect(this.url, this.urlParams);
55
       }
58
       }
56
     };
59
     };
57
 
60
 
58
     request.onError = (response) => {
61
     request.onError = (response) => {
59
-      console.log("ws连接错误",this.isHandler);
60
-      this.heartCheck.reset();
61
-      if (!this.isHandler) {
62
+      console.log("ws连接错误", this.isHandler);
63
+      this.ws = null;
64
+      if (response.status === 0) {
62
         this.reconnect(this.url, this.urlParams);
65
         this.reconnect(this.url, this.urlParams);
63
       }
66
       }
64
     };
67
     };
65
-
66
     this.ws = Atmosphere.subscribe(request);
68
     this.ws = Atmosphere.subscribe(request);
67
     return this.subject.asObservable();
69
     return this.subject.asObservable();
68
   }
70
   }
@@ -70,14 +72,14 @@ export class WebsocketMainService {
70
   private reconnect(url, data) {
72
   private reconnect(url, data) {
71
     if (this.lockReconnect) return;
73
     if (this.lockReconnect) return;
72
     this.lockReconnect = true;
74
     this.lockReconnect = true;
73
-    setTimeout(() => {
75
+    this.reconnectTimer = setTimeout(() => {
74
       //没连接上会一直重连,设置延迟避免请求过多
76
       //没连接上会一直重连,设置延迟避免请求过多
75
       this.connectWs(url, data);
77
       this.connectWs(url, data);
76
       this.lockReconnect = false;
78
       this.lockReconnect = false;
77
     }, 15000);
79
     }, 15000);
78
   }
80
   }
79
   private heartCheck = {
81
   private heartCheck = {
80
-    timeout: 60000, //1分钟发一次心跳
82
+    timeout: 30000, //发一次心跳的间隔时间
81
     timeoutObj: null,
83
     timeoutObj: null,
82
     serverTimeoutObj: null,
84
     serverTimeoutObj: null,
83
     reset: function () {
85
     reset: function () {
@@ -89,7 +91,9 @@ export class WebsocketMainService {
89
       this.timeoutObj = setTimeout(() => {
91
       this.timeoutObj = setTimeout(() => {
90
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
92
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
         //onmessage拿到返回的心跳就说明连接正常
93
         //onmessage拿到返回的心跳就说明连接正常
92
-        _this.ws.push("ping");
94
+        if (_this.ws) {
95
+          _this.ws.push("ping");
96
+        }
93
         this.serverTimeoutObj = setTimeout(() => {
97
         this.serverTimeoutObj = setTimeout(() => {
94
           //如果超过一定时间还没重置,说明后端主动断开了
98
           //如果超过一定时间还没重置,说明后端主动断开了
95
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
99
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
@@ -101,6 +105,11 @@ export class WebsocketMainService {
101
   // 断开websocket
105
   // 断开websocket
102
   closeWs(flag: boolean = false) {
106
   closeWs(flag: boolean = false) {
103
     this.isHandler = flag;
107
     this.isHandler = flag;
104
-    this.ws.close();
108
+    if (flag) {
109
+      clearTimeout(this.reconnectTimer);
110
+    }
111
+    if (this.ws) {
112
+      this.ws.close();
113
+    }
105
   }
114
   }
106
 }
115
 }

+ 26 - 17
src/app/services/websocket-nurse.service.ts

@@ -6,29 +6,32 @@ import * as Atmosphere from "atmosphere.js";
6
   providedIn: "root",
6
   providedIn: "root",
7
 })
7
 })
8
 export class WebsocketNurseService {
8
 export class WebsocketNurseService {
9
-  constructor() {}
9
+  constructor() { }
10
   private lockReconnect = false; //避免ws重复连接
10
   private lockReconnect = false; //避免ws重复连接
11
-  ws; //定义websocket
11
+  ws = null; //定义websocket
12
   private url = ""; //ws连接地址
12
   private url = ""; //ws连接地址
13
   private urlParams; //ws连接传参
13
   private urlParams; //ws连接传参
14
   private isHandler = false; //是否手动
14
   private isHandler = false; //是否手动
15
   private subject;
15
   private subject;
16
+  private reconnectTimer = null;//重连的计时器
16
   // 连接websocket
17
   // 连接websocket
17
   connectWs(url, data): Observable<any> {
18
   connectWs(url, data): Observable<any> {
18
     this.url = url;
19
     this.url = url;
19
     this.urlParams = data;
20
     this.urlParams = data;
20
     this.subject = new Subject<any>();
21
     this.subject = new Subject<any>();
21
     let request: any = {
22
     let request: any = {
22
-      url,
23
-      contentType: "application/json",
24
-      shared: true, // 标签共享
23
+      webSocketUrl: url,
25
       trackMessageLength: true, //校验数据完整性
24
       trackMessageLength: true, //校验数据完整性
26
       transport: "websocket",
25
       transport: "websocket",
26
+      handleOnlineOffline: false,
27
+      suspend: false,
28
+      maxReconnectOnClose: 0,
27
     };
29
     };
28
 
30
 
29
     request.onOpen = (response) => {
31
     request.onOpen = (response) => {
30
       console.log("ws连接成功" + new Date().toLocaleString());
32
       console.log("ws连接成功" + new Date().toLocaleString());
31
       this.heartCheck.reset().start(this);
33
       this.heartCheck.reset().start(this);
34
+      console.log(response);
32
       this.ws.push(JSON.stringify(data));
35
       this.ws.push(JSON.stringify(data));
33
     };
36
     };
34
 
37
 
@@ -36,7 +39,6 @@ export class WebsocketNurseService {
36
       this.heartCheck.reset().start(this);
39
       this.heartCheck.reset().start(this);
37
       let message = response.responseBody;
40
       let message = response.responseBody;
38
       try {
41
       try {
39
-        // alert(message);
40
         console.log("收到消息" + message);
42
         console.log("收到消息" + message);
41
         if (message !== "pong") {
43
         if (message !== "pong") {
42
           this.subject.next(JSON.parse(message));
44
           this.subject.next(JSON.parse(message));
@@ -48,21 +50,21 @@ export class WebsocketNurseService {
48
     };
50
     };
49
 
51
 
50
     request.onClose = (response) => {
52
     request.onClose = (response) => {
51
-      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
-      this.heartCheck.reset();
53
-      if (!this.isHandler) {
53
+      console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
54
+      this.ws = null;
55
+      console.log(response)
56
+      if (!this.isHandler && response.status === 408) {
54
         this.reconnect(this.url, this.urlParams);
57
         this.reconnect(this.url, this.urlParams);
55
       }
58
       }
56
     };
59
     };
57
 
60
 
58
     request.onError = (response) => {
61
     request.onError = (response) => {
59
-      console.log("ws连接错误",this.isHandler);
60
-      this.heartCheck.reset();
61
-      if (!this.isHandler) {
62
+      console.log("ws连接错误", this.isHandler);
63
+      this.ws = null;
64
+      if (response.status === 0) {
62
         this.reconnect(this.url, this.urlParams);
65
         this.reconnect(this.url, this.urlParams);
63
       }
66
       }
64
     };
67
     };
65
-
66
     this.ws = Atmosphere.subscribe(request);
68
     this.ws = Atmosphere.subscribe(request);
67
     return this.subject.asObservable();
69
     return this.subject.asObservable();
68
   }
70
   }
@@ -70,14 +72,14 @@ export class WebsocketNurseService {
70
   private reconnect(url, data) {
72
   private reconnect(url, data) {
71
     if (this.lockReconnect) return;
73
     if (this.lockReconnect) return;
72
     this.lockReconnect = true;
74
     this.lockReconnect = true;
73
-    setTimeout(() => {
75
+    this.reconnectTimer = setTimeout(() => {
74
       //没连接上会一直重连,设置延迟避免请求过多
76
       //没连接上会一直重连,设置延迟避免请求过多
75
       this.connectWs(url, data);
77
       this.connectWs(url, data);
76
       this.lockReconnect = false;
78
       this.lockReconnect = false;
77
     }, 15000);
79
     }, 15000);
78
   }
80
   }
79
   private heartCheck = {
81
   private heartCheck = {
80
-    timeout: 60000, //1分钟发一次心跳
82
+    timeout: 30000, //发一次心跳的间隔时间
81
     timeoutObj: null,
83
     timeoutObj: null,
82
     serverTimeoutObj: null,
84
     serverTimeoutObj: null,
83
     reset: function () {
85
     reset: function () {
@@ -89,7 +91,9 @@ export class WebsocketNurseService {
89
       this.timeoutObj = setTimeout(() => {
91
       this.timeoutObj = setTimeout(() => {
90
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
92
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
         //onmessage拿到返回的心跳就说明连接正常
93
         //onmessage拿到返回的心跳就说明连接正常
92
-        _this.ws.push("ping");
94
+        if (_this.ws) {
95
+          _this.ws.push("ping");
96
+        }
93
         this.serverTimeoutObj = setTimeout(() => {
97
         this.serverTimeoutObj = setTimeout(() => {
94
           //如果超过一定时间还没重置,说明后端主动断开了
98
           //如果超过一定时间还没重置,说明后端主动断开了
95
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
99
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
@@ -101,6 +105,11 @@ export class WebsocketNurseService {
101
   // 断开websocket
105
   // 断开websocket
102
   closeWs(flag: boolean = false) {
106
   closeWs(flag: boolean = false) {
103
     this.isHandler = flag;
107
     this.isHandler = flag;
104
-    this.ws.close();
108
+    if (flag) {
109
+      clearTimeout(this.reconnectTimer);
110
+    }
111
+    if (this.ws) {
112
+      this.ws.close();
113
+    }
105
   }
114
   }
106
 }
115
 }

+ 26 - 17
src/app/services/websocket-phone.service.ts

@@ -6,29 +6,32 @@ import * as Atmosphere from "atmosphere.js";
6
   providedIn: "root",
6
   providedIn: "root",
7
 })
7
 })
8
 export class WebsocketPhoneService {
8
 export class WebsocketPhoneService {
9
-  constructor() {}
9
+  constructor() { }
10
   private lockReconnect = false; //避免ws重复连接
10
   private lockReconnect = false; //避免ws重复连接
11
-  ws; //定义websocket
11
+  ws = null; //定义websocket
12
   private url = ""; //ws连接地址
12
   private url = ""; //ws连接地址
13
   private urlParams; //ws连接传参
13
   private urlParams; //ws连接传参
14
   private isHandler = false; //是否手动
14
   private isHandler = false; //是否手动
15
   private subject;
15
   private subject;
16
+  private reconnectTimer = null;//重连的计时器
16
   // 连接websocket
17
   // 连接websocket
17
   connectWs(url, data): Observable<any> {
18
   connectWs(url, data): Observable<any> {
18
     this.url = url;
19
     this.url = url;
19
     this.urlParams = data;
20
     this.urlParams = data;
20
     this.subject = new Subject<any>();
21
     this.subject = new Subject<any>();
21
     let request: any = {
22
     let request: any = {
22
-      url,
23
-      contentType: "application/json",
24
-      shared: true, // 标签共享
23
+      webSocketUrl: url,
25
       trackMessageLength: true, //校验数据完整性
24
       trackMessageLength: true, //校验数据完整性
26
       transport: "websocket",
25
       transport: "websocket",
26
+      handleOnlineOffline: false,
27
+      suspend: false,
28
+      maxReconnectOnClose: 0,
27
     };
29
     };
28
 
30
 
29
     request.onOpen = (response) => {
31
     request.onOpen = (response) => {
30
       console.log("ws连接成功" + new Date().toLocaleString());
32
       console.log("ws连接成功" + new Date().toLocaleString());
31
       this.heartCheck.reset().start(this);
33
       this.heartCheck.reset().start(this);
34
+      console.log(response);
32
       this.ws.push(JSON.stringify(data));
35
       this.ws.push(JSON.stringify(data));
33
     };
36
     };
34
 
37
 
@@ -36,7 +39,6 @@ export class WebsocketPhoneService {
36
       this.heartCheck.reset().start(this);
39
       this.heartCheck.reset().start(this);
37
       let message = response.responseBody;
40
       let message = response.responseBody;
38
       try {
41
       try {
39
-        // alert(message);
40
         console.log("收到消息" + message);
42
         console.log("收到消息" + message);
41
         if (message !== "pong") {
43
         if (message !== "pong") {
42
           this.subject.next(JSON.parse(message));
44
           this.subject.next(JSON.parse(message));
@@ -48,21 +50,21 @@ export class WebsocketPhoneService {
48
     };
50
     };
49
 
51
 
50
     request.onClose = (response) => {
52
     request.onClose = (response) => {
51
-      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
-      this.heartCheck.reset();
53
-      if (!this.isHandler) {
53
+      console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
54
+      this.ws = null;
55
+      console.log(response)
56
+      if (!this.isHandler && response.status === 408) {
54
         this.reconnect(this.url, this.urlParams);
57
         this.reconnect(this.url, this.urlParams);
55
       }
58
       }
56
     };
59
     };
57
 
60
 
58
     request.onError = (response) => {
61
     request.onError = (response) => {
59
-      console.log("ws连接错误",this.isHandler);
60
-      this.heartCheck.reset();
61
-      if (!this.isHandler) {
62
+      console.log("ws连接错误", this.isHandler);
63
+      this.ws = null;
64
+      if (response.status === 0) {
62
         this.reconnect(this.url, this.urlParams);
65
         this.reconnect(this.url, this.urlParams);
63
       }
66
       }
64
     };
67
     };
65
-
66
     this.ws = Atmosphere.subscribe(request);
68
     this.ws = Atmosphere.subscribe(request);
67
     return this.subject.asObservable();
69
     return this.subject.asObservable();
68
   }
70
   }
@@ -70,14 +72,14 @@ export class WebsocketPhoneService {
70
   private reconnect(url, data) {
72
   private reconnect(url, data) {
71
     if (this.lockReconnect) return;
73
     if (this.lockReconnect) return;
72
     this.lockReconnect = true;
74
     this.lockReconnect = true;
73
-    setTimeout(() => {
75
+    this.reconnectTimer = setTimeout(() => {
74
       //没连接上会一直重连,设置延迟避免请求过多
76
       //没连接上会一直重连,设置延迟避免请求过多
75
       this.connectWs(url, data);
77
       this.connectWs(url, data);
76
       this.lockReconnect = false;
78
       this.lockReconnect = false;
77
     }, 15000);
79
     }, 15000);
78
   }
80
   }
79
   private heartCheck = {
81
   private heartCheck = {
80
-    timeout: 60000, //1分钟发一次心跳
82
+    timeout: 30000, //发一次心跳的间隔时间
81
     timeoutObj: null,
83
     timeoutObj: null,
82
     serverTimeoutObj: null,
84
     serverTimeoutObj: null,
83
     reset: function () {
85
     reset: function () {
@@ -89,7 +91,9 @@ export class WebsocketPhoneService {
89
       this.timeoutObj = setTimeout(() => {
91
       this.timeoutObj = setTimeout(() => {
90
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
92
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
         //onmessage拿到返回的心跳就说明连接正常
93
         //onmessage拿到返回的心跳就说明连接正常
92
-        _this.ws.push("ping");
94
+        if (_this.ws) {
95
+          _this.ws.push("ping");
96
+        }
93
         this.serverTimeoutObj = setTimeout(() => {
97
         this.serverTimeoutObj = setTimeout(() => {
94
           //如果超过一定时间还没重置,说明后端主动断开了
98
           //如果超过一定时间还没重置,说明后端主动断开了
95
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
99
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
@@ -101,6 +105,11 @@ export class WebsocketPhoneService {
101
   // 断开websocket
105
   // 断开websocket
102
   closeWs(flag: boolean = false) {
106
   closeWs(flag: boolean = false) {
103
     this.isHandler = flag;
107
     this.isHandler = flag;
104
-    this.ws.close();
108
+    if (flag) {
109
+      clearTimeout(this.reconnectTimer);
110
+    }
111
+    if (this.ws) {
112
+      this.ws.close();
113
+    }
105
   }
114
   }
106
 }
115
 }

+ 26 - 17
src/app/services/websocket-ser.service.ts

@@ -6,29 +6,32 @@ import * as Atmosphere from "atmosphere.js";
6
   providedIn: "root",
6
   providedIn: "root",
7
 })
7
 })
8
 export class WebsocketSerService {
8
 export class WebsocketSerService {
9
-  constructor() {}
9
+  constructor() { }
10
   private lockReconnect = false; //避免ws重复连接
10
   private lockReconnect = false; //避免ws重复连接
11
-  ws; //定义websocket
11
+  ws = null; //定义websocket
12
   private url = ""; //ws连接地址
12
   private url = ""; //ws连接地址
13
   private urlParams; //ws连接传参
13
   private urlParams; //ws连接传参
14
   private isHandler = false; //是否手动
14
   private isHandler = false; //是否手动
15
   private subject;
15
   private subject;
16
+  private reconnectTimer = null;//重连的计时器
16
   // 连接websocket
17
   // 连接websocket
17
   connectWs(url, data): Observable<any> {
18
   connectWs(url, data): Observable<any> {
18
     this.url = url;
19
     this.url = url;
19
     this.urlParams = data;
20
     this.urlParams = data;
20
     this.subject = new Subject<any>();
21
     this.subject = new Subject<any>();
21
     let request: any = {
22
     let request: any = {
22
-      url,
23
-      contentType: "application/json",
24
-      shared: true, // 标签共享
23
+      webSocketUrl: url,
25
       trackMessageLength: true, //校验数据完整性
24
       trackMessageLength: true, //校验数据完整性
26
       transport: "websocket",
25
       transport: "websocket",
26
+      handleOnlineOffline: false,
27
+      suspend: false,
28
+      maxReconnectOnClose: 0,
27
     };
29
     };
28
 
30
 
29
     request.onOpen = (response) => {
31
     request.onOpen = (response) => {
30
       console.log("ws连接成功" + new Date().toLocaleString());
32
       console.log("ws连接成功" + new Date().toLocaleString());
31
       this.heartCheck.reset().start(this);
33
       this.heartCheck.reset().start(this);
34
+      console.log(response);
32
       this.ws.push(JSON.stringify(data));
35
       this.ws.push(JSON.stringify(data));
33
     };
36
     };
34
 
37
 
@@ -36,7 +39,6 @@ export class WebsocketSerService {
36
       this.heartCheck.reset().start(this);
39
       this.heartCheck.reset().start(this);
37
       let message = response.responseBody;
40
       let message = response.responseBody;
38
       try {
41
       try {
39
-        // alert(message);
40
         console.log("收到消息" + message);
42
         console.log("收到消息" + message);
41
         if (message !== "pong") {
43
         if (message !== "pong") {
42
           this.subject.next(JSON.parse(message));
44
           this.subject.next(JSON.parse(message));
@@ -48,21 +50,21 @@ export class WebsocketSerService {
48
     };
50
     };
49
 
51
 
50
     request.onClose = (response) => {
52
     request.onClose = (response) => {
51
-      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
-      this.heartCheck.reset();
53
-      if (!this.isHandler) {
53
+      console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
54
+      this.ws = null;
55
+      console.log(response)
56
+      if (!this.isHandler && response.status === 408) {
54
         this.reconnect(this.url, this.urlParams);
57
         this.reconnect(this.url, this.urlParams);
55
       }
58
       }
56
     };
59
     };
57
 
60
 
58
     request.onError = (response) => {
61
     request.onError = (response) => {
59
-      console.log("ws连接错误",this.isHandler);
60
-      this.heartCheck.reset();
61
-      if (!this.isHandler) {
62
+      console.log("ws连接错误", this.isHandler);
63
+      this.ws = null;
64
+      if (response.status === 0) {
62
         this.reconnect(this.url, this.urlParams);
65
         this.reconnect(this.url, this.urlParams);
63
       }
66
       }
64
     };
67
     };
65
-
66
     this.ws = Atmosphere.subscribe(request);
68
     this.ws = Atmosphere.subscribe(request);
67
     return this.subject.asObservable();
69
     return this.subject.asObservable();
68
   }
70
   }
@@ -70,14 +72,14 @@ export class WebsocketSerService {
70
   private reconnect(url, data) {
72
   private reconnect(url, data) {
71
     if (this.lockReconnect) return;
73
     if (this.lockReconnect) return;
72
     this.lockReconnect = true;
74
     this.lockReconnect = true;
73
-    setTimeout(() => {
75
+    this.reconnectTimer = setTimeout(() => {
74
       //没连接上会一直重连,设置延迟避免请求过多
76
       //没连接上会一直重连,设置延迟避免请求过多
75
       this.connectWs(url, data);
77
       this.connectWs(url, data);
76
       this.lockReconnect = false;
78
       this.lockReconnect = false;
77
     }, 15000);
79
     }, 15000);
78
   }
80
   }
79
   private heartCheck = {
81
   private heartCheck = {
80
-    timeout: 60000, //1分钟发一次心跳
82
+    timeout: 30000, //发一次心跳的间隔时间
81
     timeoutObj: null,
83
     timeoutObj: null,
82
     serverTimeoutObj: null,
84
     serverTimeoutObj: null,
83
     reset: function () {
85
     reset: function () {
@@ -89,7 +91,9 @@ export class WebsocketSerService {
89
       this.timeoutObj = setTimeout(() => {
91
       this.timeoutObj = setTimeout(() => {
90
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
92
         //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
         //onmessage拿到返回的心跳就说明连接正常
93
         //onmessage拿到返回的心跳就说明连接正常
92
-        _this.ws.push("ping");
94
+        if (_this.ws) {
95
+          _this.ws.push("ping");
96
+        }
93
         this.serverTimeoutObj = setTimeout(() => {
97
         this.serverTimeoutObj = setTimeout(() => {
94
           //如果超过一定时间还没重置,说明后端主动断开了
98
           //如果超过一定时间还没重置,说明后端主动断开了
95
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
99
           _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
@@ -101,6 +105,11 @@ export class WebsocketSerService {
101
   // 断开websocket
105
   // 断开websocket
102
   closeWs(flag: boolean = false) {
106
   closeWs(flag: boolean = false) {
103
     this.isHandler = flag;
107
     this.isHandler = flag;
104
-    this.ws.close();
108
+    if (flag) {
109
+      clearTimeout(this.reconnectTimer);
110
+    }
111
+    if (this.ws) {
112
+      this.ws.close();
113
+    }
105
   }
114
   }
106
 }
115
 }

+ 12 - 3
src/app/views/fuwutai/fuwutai.component.html

@@ -579,7 +579,7 @@
579
     <div class="left"
579
     <div class="left"
580
       *ngIf="fixedTab!=''&&showLastItems&&fixedTab!='newOrder'&&fixedTab!='toSystem'&&fixedTab!='logout'">
580
       *ngIf="fixedTab!=''&&showLastItems&&fixedTab!='newOrder'&&fixedTab!='toSystem'&&fixedTab!='logout'">
581
       <!-- 绑定分机 -->
581
       <!-- 绑定分机 -->
582
-      <div class="con bindingExtension" *ngIf="(fixedTab=='bindingExtension'&&fixedTab!='newOrder')">
582
+      <div class="con bindingExtension" *ngIf="(fixedTab=='bindingExtension'&&fixedTab!='newOrder'&&!phoneNumLoading)">
583
         <div class="title">绑定分机</div>
583
         <div class="title">绑定分机</div>
584
         <overlay-scrollbars #osComponentRef7 [ngStyle]="{ height:'152px' }" class="conditions">
584
         <overlay-scrollbars #osComponentRef7 [ngStyle]="{ height:'152px' }" class="conditions">
585
           <div nz-row *ngIf="binding === 1">
585
           <div nz-row *ngIf="binding === 1">
@@ -618,7 +618,16 @@
618
             *ngIf="binding === 3">绑定分机</button>
618
             *ngIf="binding === 3">绑定分机</button>
619
           <button nz-button nzType="primary" nzValue="small" (click)="bindingHandle()"
619
           <button nz-button nzType="primary" nzValue="small" (click)="bindingHandle()"
620
             *ngIf="binding === 2">重新绑定</button>
620
             *ngIf="binding === 2">重新绑定</button>
621
-          <button nz-button nzType="primary" nzValue="small" *ngIf="binding === 1" (click)="bindingOk()">确定绑定</button>
621
+          <button nz-button nzType="primary" nzValue="small" *ngIf="binding === 1" (click)="bindingOk()" [nzLoading]="bindOkLoading">确定绑定</button>
622
+        </div>
623
+      </div>
624
+      <div class="con bindingExtension display_flex justify-content_flex-center align-items_center"
625
+        *ngIf="(fixedTab=='bindingExtension'&&fixedTab!='newOrder'&&phoneNumLoading)">
626
+        <div class="loadingFull display_flex justify-content_flex-center align-items_center">
627
+          <div class="loadingFullInner">
628
+            <img src="../../../assets/images/loading.gif" alt="">
629
+            <div>加载中...</div>
630
+          </div>
622
         </div>
631
         </div>
623
       </div>
632
       </div>
624
       <!-- 工单范围 -->
633
       <!-- 工单范围 -->
@@ -1446,4 +1455,4 @@
1446
 <app-select-dept [createLoading]="createLoading" [deptFlag]="deptFlag" (submitFormHand)="submitFormHand($event)"
1455
 <app-select-dept [createLoading]="createLoading" [deptFlag]="deptFlag" (submitFormHand)="submitFormHand($event)"
1447
   (deptFlagHand)="deptFlagHand($event)" [hosId]="checkedHos" *ngIf="deptFlag"></app-select-dept>
1456
   (deptFlagHand)="deptFlagHand($event)" [hosId]="checkedHos" *ngIf="deptFlag"></app-select-dept>
1448
 
1457
 
1449
-<router-outlet (deactivate)="refreshList($event)"></router-outlet>
1458
+<router-outlet (deactivate)="refreshList($event)"></router-outlet>

+ 65 - 50
src/app/views/fuwutai/fuwutai.component.ts

@@ -106,7 +106,7 @@ export class FuwutaiComponent implements OnInit {
106
     private notification: NzNotificationService,
106
     private notification: NzNotificationService,
107
     private phones: WebsocketPhoneService,
107
     private phones: WebsocketPhoneService,
108
     private sers: WebsocketSerService
108
     private sers: WebsocketSerService
109
-  ) {}
109
+  ) { }
110
   validateFormZy!: FormGroup;
110
   validateFormZy!: FormGroup;
111
   validateFormQt!: FormGroup;
111
   validateFormQt!: FormGroup;
112
   // ---------------------------------------
112
   // ---------------------------------------
@@ -267,11 +267,11 @@ export class FuwutaiComponent implements OnInit {
267
     });
267
     });
268
     this.router.navigateByUrl(
268
     this.router.navigateByUrl(
269
       "dispatchingDesk/allotWorker/" +
269
       "dispatchingDesk/allotWorker/" +
270
-        ids_types.join("-") +
271
-        "/" +
272
-        stateIds.join("-") +
273
-        "/2/" +
274
-        this.checkedHos
270
+      ids_types.join("-") +
271
+      "/" +
272
+      stateIds.join("-") +
273
+      "/2/" +
274
+      this.checkedHos
275
     );
275
     );
276
   }
276
   }
277
   // 批量撤回
277
   // 批量撤回
@@ -355,8 +355,8 @@ export class FuwutaiComponent implements OnInit {
355
     if (newOrderShowFlag) {
355
     if (newOrderShowFlag) {
356
       let goods = this.validateFormZy.controls.goods.value
356
       let goods = this.validateFormZy.controls.goods.value
357
         ? this.validateFormZy.controls.goods.value.filter(
357
         ? this.validateFormZy.controls.goods.value.filter(
358
-            (item) => item.checked === true
359
-          )
358
+          (item) => item.checked === true
359
+        )
360
         : [];
360
         : [];
361
       goods = goods.map((current) => current.value).join();
361
       goods = goods.map((current) => current.value).join();
362
       let postData = {
362
       let postData = {
@@ -540,12 +540,8 @@ export class FuwutaiComponent implements OnInit {
540
   }
540
   }
541
   ngOnDestroy() {
541
   ngOnDestroy() {
542
     clearInterval(this.refreshTimerId);
542
     clearInterval(this.refreshTimerId);
543
-    if (this.phones.ws) {
544
-      this.phones.closeWs(true);
545
-    }
546
-    if (this.sers.ws) {
547
-      this.sers.closeWs(true);
548
-    }
543
+    this.phones.closeWs(true);
544
+    this.sers.closeWs(true);
549
   }
545
   }
550
   unassignedSearchCon: string = ""; //未分派搜索框内容
546
   unassignedSearchCon: string = ""; //未分派搜索框内容
551
   arriveSearchCon: string = ""; //待到达搜索框内容
547
   arriveSearchCon: string = ""; //待到达搜索框内容
@@ -620,18 +616,23 @@ export class FuwutaiComponent implements OnInit {
620
       });
616
       });
621
   }
617
   }
622
   // 连接websocket---phone
618
   // 连接websocket---phone
619
+  bindOkLoading = false;
623
   getWebsocketPhone() {
620
   getWebsocketPhone() {
621
+    this.bindOkLoading = true;
624
     let userCount =
622
     let userCount =
625
       this.user.user.account + "|" + this.phoneNumListSelected.join("_");
623
       this.user.user.account + "|" + this.phoneNumListSelected.join("_");
626
     this.phones.connectWs(http.phoneWs, { userCount }).subscribe((data) => {
624
     this.phones.connectWs(http.phoneWs, { userCount }).subscribe((data) => {
627
       // data = { status: 201, phone: '013581394341' };//ceshi
625
       // data = { status: 201, phone: '013581394341' };//ceshi
628
       console.log(data);
626
       console.log(data);
627
+      this.bindOkLoading = false;
629
       if (data.status == 200 && data.phone) {
628
       if (data.status == 200 && data.phone) {
630
         localStorage.setItem("phones", data.phone);
629
         localStorage.setItem("phones", data.phone);
631
         this.phoneNumListBind = data.phone.split("_");
630
         this.phoneNumListBind = data.phone.split("_");
632
         this.phoneNumListBind.forEach((item) => {
631
         this.phoneNumListBind.forEach((item) => {
633
-          this.phoneNumList.find((item1) => item1.value === item).checked =
634
-            true;
632
+          let obj = this.phoneNumList.find((item1) => item1.value === item);
633
+          if (obj) {
634
+            obj.checked = true;
635
+          }
635
         });
636
         });
636
         this.binding = 2;
637
         this.binding = 2;
637
       } else if (data.status == 201 && data.phone) {
638
       } else if (data.status == 201 && data.phone) {
@@ -683,19 +684,16 @@ export class FuwutaiComponent implements OnInit {
683
   //确定绑定
684
   //确定绑定
684
   bindingOk() {
685
   bindingOk() {
685
     if (this.phoneNumListSelected.length > 0) {
686
     if (this.phoneNumListSelected.length > 0) {
686
-      if (this.phones.ws) {
687
-        this.phones.closeWs(true);
688
-      }
687
+      this.phones.closeWs(true);
689
       this.getWebsocketPhone();
688
       this.getWebsocketPhone();
690
     } else {
689
     } else {
691
       localStorage.removeItem("phones");
690
       localStorage.removeItem("phones");
692
       this.binding = 3;
691
       this.binding = 3;
692
+      this.phoneNumListBind = [];
693
       this.phoneNumList.forEach((item) => {
693
       this.phoneNumList.forEach((item) => {
694
         item.checked = false;
694
         item.checked = false;
695
       });
695
       });
696
-      if (this.phones.ws) {
697
-        this.phones.closeWs(true);
698
-      }
696
+      this.phones.closeWs(true);
699
     }
697
     }
700
   }
698
   }
701
   // 选择分机
699
   // 选择分机
@@ -1072,20 +1070,20 @@ export class FuwutaiComponent implements OnInit {
1072
     if (flag == 1) {
1070
     if (flag == 1) {
1073
       this.router.navigateByUrl(
1071
       this.router.navigateByUrl(
1074
         "dispatchingDesk/allotWorker/" +
1072
         "dispatchingDesk/allotWorker/" +
1075
-          id +
1076
-          "/" +
1077
-          stateId +
1078
-          "/1/" +
1079
-          this.checkedHos
1073
+        id +
1074
+        "/" +
1075
+        stateId +
1076
+        "/1/" +
1077
+        this.checkedHos
1080
       );
1078
       );
1081
     } else {
1079
     } else {
1082
       this.router.navigateByUrl(
1080
       this.router.navigateByUrl(
1083
         "dispatchingDesk/allotWorker/" +
1081
         "dispatchingDesk/allotWorker/" +
1084
-          id +
1085
-          "/" +
1086
-          stateId +
1087
-          "/0/" +
1088
-          this.checkedHos
1082
+        id +
1083
+        "/" +
1084
+        stateId +
1085
+        "/0/" +
1086
+        this.checkedHos
1089
       );
1087
       );
1090
     }
1088
     }
1091
   }
1089
   }
@@ -1277,9 +1275,10 @@ export class FuwutaiComponent implements OnInit {
1277
     } else {
1275
     } else {
1278
       this.binding = 3; //绑定分机
1276
       this.binding = 3; //绑定分机
1279
     }
1277
     }
1278
+    this.getPhoneNumList();
1280
   }
1279
   }
1281
-  //获取所有分机号码列表,flag表示不自动连接
1282
-  getPhoneNum(flag = true) {
1280
+  //获取所有分机号码列表
1281
+  getPhoneNumList() {
1283
     this.phoneNumLoading = true;
1282
     this.phoneNumLoading = true;
1284
     let postData = {
1283
     let postData = {
1285
       idx: 0,
1284
       idx: 0,
@@ -1296,24 +1295,42 @@ export class FuwutaiComponent implements OnInit {
1296
         this.phoneNumList.map((item) => {
1295
         this.phoneNumList.map((item) => {
1297
           return { ...item, checked: false };
1296
           return { ...item, checked: false };
1298
         });
1297
         });
1299
-        if (!flag) {
1300
-          //切换院区的时候清除掉缓存中的号码
1301
-          localStorage.removeItem("phones");
1302
-        }
1303
-        this.phoneNumListBind = localStorage.getItem("phones")
1304
-          ? localStorage.getItem("phones").split("_")
1305
-          : [];
1306
-        this.phoneNumListSelected = localStorage.getItem("phones")
1307
-          ? localStorage.getItem("phones").split("_")
1308
-          : [];
1309
-        if (this.phoneNumListBind.length) {
1310
-          this.getWebsocketPhone();
1311
-        }
1298
+        this.phoneNumListBind.forEach((item) => {
1299
+          let obj = this.phoneNumList.find((item1) => item1.value === item);
1300
+          if (obj) {
1301
+            obj.checked = true;
1302
+          }
1303
+        });
1312
       });
1304
       });
1313
   }
1305
   }
1306
+  getPhoneNum(flag = true) {
1307
+    if (!flag) {
1308
+      //切换院区的时候清除掉缓存中的号码
1309
+      localStorage.removeItem("phones");
1310
+    }
1311
+    this.phoneNumListBind = localStorage.getItem("phones")
1312
+      ? localStorage.getItem("phones").split("_")
1313
+      : [];
1314
+    this.phoneNumListSelected = localStorage.getItem("phones")
1315
+      ? localStorage.getItem("phones").split("_")
1316
+      : [];
1317
+    if (this.phoneNumListBind.length) {
1318
+      this.getWebsocketPhone();
1319
+    }
1320
+  }
1314
   // 绑定分机,重新绑定
1321
   // 绑定分机,重新绑定
1315
   bindingHandle() {
1322
   bindingHandle() {
1316
     this.binding = 1;
1323
     this.binding = 1;
1324
+    this.phoneNumList.forEach((item) => {
1325
+      item.checked = false;
1326
+    })
1327
+    this.phoneNumList.forEach((item) => {
1328
+      this.phoneNumListBind.forEach(v => {
1329
+        if (item.value == v) {
1330
+          item.checked = true;
1331
+        }
1332
+      })
1333
+    });
1317
   }
1334
   }
1318
   // 点击tab切换
1335
   // 点击tab切换
1319
   tabClick(key) {
1336
   tabClick(key) {
@@ -2174,9 +2191,7 @@ export class FuwutaiComponent implements OnInit {
2174
         if (data.status == 200) {
2191
         if (data.status == 200) {
2175
           that.showPromptModal("保存", true, "");
2192
           that.showPromptModal("保存", true, "");
2176
           // 关闭phone的websocket
2193
           // 关闭phone的websocket
2177
-          if (this.phones.ws) {
2178
-            this.phones.closeWs(true);
2179
-          }
2194
+          this.phones.closeWs(true);
2180
           this.getPhoneNum(false);
2195
           this.getPhoneNum(false);
2181
           that.user.user.scope = data.data;
2196
           that.user.user.scope = data.data;
2182
           localStorage.setItem("user", JSON.stringify(that.user));
2197
           localStorage.setItem("user", JSON.stringify(that.user));