Spaces:
Paused
Paused
Update frontend/src/components/Terminal.tsx
Browse files
frontend/src/components/Terminal.tsx
CHANGED
|
@@ -1,7 +1,35 @@
|
|
| 1 |
-
import React from 'react';
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
export default Terminal;
|
|
|
|
| 1 |
+
import React, { useEffect, useRef } from 'react';
|
| 2 |
+
import { Terminal as XTerm } from 'xterm';
|
| 3 |
+
import { FitAddon } from 'xterm-addon-fit';
|
| 4 |
+
import 'xterm/css/xterm.css';
|
| 5 |
+
|
| 6 |
+
const Terminal: React.FC = () => {
|
| 7 |
+
const termRef = useRef<HTMLDivElement>(null);
|
| 8 |
+
const xterm = useRef<XTerm>();
|
| 9 |
+
const ws = useRef<WebSocket>();
|
| 10 |
+
|
| 11 |
+
useEffect(() => {
|
| 12 |
+
const term = new XTerm({ cursorBlink: true, theme: { background: '#1e1e1e' } });
|
| 13 |
+
const fit = new FitAddon();
|
| 14 |
+
term.loadAddon(fit);
|
| 15 |
+
if (termRef.current) {
|
| 16 |
+
term.open(termRef.current);
|
| 17 |
+
fit.fit();
|
| 18 |
+
xterm.current = term;
|
| 19 |
+
}
|
| 20 |
+
// Connect WebSocket to /ws/terminal
|
| 21 |
+
const socket = new WebSocket(`wss://${window.location.host}/ws/terminal`);
|
| 22 |
+
ws.current = socket;
|
| 23 |
+
socket.onmessage = (event) => {
|
| 24 |
+
term.write(event.data);
|
| 25 |
+
};
|
| 26 |
+
term.onData(data => {
|
| 27 |
+
socket.send(`exec:${data}`);
|
| 28 |
+
});
|
| 29 |
+
return () => { socket.close(); term.dispose(); };
|
| 30 |
+
}, []);
|
| 31 |
+
|
| 32 |
+
return <div ref={termRef} style={{ height: '100%', width: '100%' }} />;
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
export default Terminal;
|