From 78372d6345cf5f1e04d6a8d56c416a7aec70b998 Mon Sep 17 00:00:00 2001
From: Xiaochao Ma <maxiaochao@inspur.com>
Date: Tue, 8 Mar 2022 20:13:00 +0800
Subject: SOL: fix socket close exception

When the sol page is opened, a socket is opened;
when the sol page is closed, the socket is not closed
(console print is also exception).
It only closes when you log out.
Fix this condition to 'close socket when SOL page is exited'.

test:
Enter the SOL page-->console print'websocket console0/ opened'
-->close the SOL page
-->console print'websocket console0/ closed. code: 1000 reason: '

Signed-off-by: Xiaochao Ma <maxiaochao@inspur.com>
Change-Id: I0f406c2085aec303d6e5139d57b31ed6f244a155
---
 .../SerialOverLan/SerialOverLanConsole.vue         | 26 ++++++++++++++--------
 1 file changed, 17 insertions(+), 9 deletions(-)

(limited to 'src/views/Operations/SerialOverLan/SerialOverLanConsole.vue')

diff --git a/src/views/Operations/SerialOverLan/SerialOverLanConsole.vue b/src/views/Operations/SerialOverLan/SerialOverLanConsole.vue
index ce8549df..694083fd 100644
--- a/src/views/Operations/SerialOverLan/SerialOverLanConsole.vue
+++ b/src/views/Operations/SerialOverLan/SerialOverLanConsole.vue
@@ -85,37 +85,38 @@ export default {
   },
   beforeDestroy() {
     window.removeEventListener('resize', this.resizeConsoleWindow);
+    this.closeTerminal();
   },
   methods: {
     openTerminal() {
       const token = this.$store.getters['authentication/token'];
 
-      const ws = new WebSocket(`wss://${window.location.host}/console0`, [
+      this.ws = new WebSocket(`wss://${window.location.host}/console0`, [
         token,
       ]);
 
       // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
 
-      const term = new Terminal({
+      this.term = new Terminal({
         fontSize: 15,
         fontFamily:
           'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
       });
 
-      const attachAddon = new AttachAddon(ws);
-      term.loadAddon(attachAddon);
+      const attachAddon = new AttachAddon(this.ws);
+      this.term.loadAddon(attachAddon);
 
       const fitAddon = new FitAddon();
-      term.loadAddon(fitAddon);
+      this.term.loadAddon(fitAddon);
 
       const SOL_THEME = {
         background: '#19273c',
         cursor: 'rgba(83, 146, 255, .5)',
         scrollbar: 'rgba(83, 146, 255, .5)',
       };
-      term.setOption('theme', SOL_THEME);
+      this.term.setOption('theme', SOL_THEME);
 
-      term.open(this.$refs.panel);
+      this.term.open(this.$refs.panel);
       fitAddon.fit();
 
       this.resizeConsoleWindow = throttle(() => {
@@ -124,10 +125,10 @@ export default {
       window.addEventListener('resize', this.resizeConsoleWindow);
 
       try {
-        ws.onopen = function () {
+        this.ws.onopen = function () {
           console.log('websocket console0/ opened');
         };
-        ws.onclose = function (event) {
+        this.ws.onclose = function (event) {
           console.log(
             'websocket console0/ closed. code: ' +
               event.code +
@@ -139,6 +140,13 @@ export default {
         console.log(error);
       }
     },
+    closeTerminal() {
+      console.log('closeTerminal');
+      this.term.dispose();
+      this.term = null;
+      this.ws.close();
+      this.ws = null;
+    },
     openConsoleWindow() {
       window.open(
         '#/console/serial-over-lan-console',
-- 
cgit v1.2.3

