Kaynağa Gözat

fix: ws bug

TianChenjiang 5 yıl önce
ebeveyn
işleme
d0fc9e52cb
1 değiştirilmiş dosya ile 16 ekleme ve 15 silme
  1. 16 15
      src/pages/terminal.js

+ 16 - 15
src/pages/terminal.js

@@ -1,8 +1,8 @@
 import React, { PureComponent } from 'react';
 import { Terminal as XTerm } from 'xterm';
-import {message} from 'antd'
-import styles from './terminal.less'
-import 'xterm/css/xterm.css'
+import { message } from 'antd';
+import styles from './terminal.less';
+import 'xterm/css/xterm.css';
 import { FitAddon } from 'xterm-addon-fit';
 
 export default class Terminal extends PureComponent {
@@ -10,24 +10,25 @@ export default class Terminal extends PureComponent {
     this.term = new XTerm();
     const fitAddon = new FitAddon();
     this.term.loadAddon(fitAddon);
-    this.term.open(document.getElementById('terminal'), true)
+    this.term.open(document.getElementById('terminal'), true);
     fitAddon.fit();
     const isDev = process.env.NODE_ENV === 'development';
-    this.socket = new WebSocket(`ws://${isDev ? '172.19.240.15:8080' : window.location.host}/ws/terminal${this.props.location?.search}`);
-    this.socket.onerror = (error) => {
+    this.socket = new WebSocket(
+      `wss://${
+        isDev ? '172.19.240.15:8080' : window.location.host
+      }/ws/terminal${this.props.location?.search}`,
+    );
+    this.socket.onerror = error => {
       message.error('连接错误!请尝试重新登陆或重新打开终端!');
-    }
-    this.socket.onmessage = (event) => {
+    };
+    this.socket.onmessage = event => {
       this.term.write(event.data);
-    }
-    this.term.onData((data) => {
+    };
+    this.term.onData(data => {
       this.socket.send(data);
-    })
+    });
   }
   render() {
-    return (
-      <div id="terminal" style={{height: '100%', width: '100%'}}>
-      </div>
-    );
+    return <div id="terminal" style={{ height: '100%', width: '100%' }}></div>;
   }
 }