Skip to content

Commit e0838c8

Browse files
committed
First pass at connecting to console and rendering the output from the server.
1 parent 6db9f65 commit e0838c8

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

resources/scripts/components/server/Console.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as TerminalFit from 'xterm/lib/addons/fit/fit';
44
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
55
import { State, useStoreState } from 'easy-peasy';
66
import { ApplicationState } from '@/state/types';
7+
import { connect } from 'formik';
78

89
const theme = {
910
background: 'transparent',
@@ -27,7 +28,7 @@ const theme = {
2728
};
2829

2930
export default () => {
30-
const connected = useStoreState((state: State<ApplicationState>) => state.server.socket.connected);
31+
const { instance, connected } = useStoreState((state: State<ApplicationState>) => state.server.socket);
3132

3233
const ref = createRef<HTMLDivElement>();
3334
const terminal = useRef(new Terminal({
@@ -40,17 +41,34 @@ export default () => {
4041
theme: theme,
4142
}));
4243

44+
const handleServerLog = (lines: string[]) => lines.forEach(data => {
45+
return data.split(/\n/g).forEach(line => terminal.current.writeln(line + '\u001b[0m'));
46+
});
47+
48+
const handleConsoleOutput = (line: string) => terminal.current.writeln(line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m');
49+
4350
useEffect(() => {
4451
ref.current && terminal.current.open(ref.current);
4552

4653
// @see https://github.com/xtermjs/xterm.js/issues/2265
4754
// @see https://github.com/xtermjs/xterm.js/issues/2230
4855
TerminalFit.fit(terminal.current);
49-
50-
terminal.current.writeln('Testing console data');
51-
terminal.current.writeln('Testing other data');
5256
}, []);
5357

58+
useEffect(() => {
59+
if (connected && instance) {
60+
instance.addListener('server log', handleServerLog);
61+
instance.addListener('console output', handleConsoleOutput);
62+
}
63+
}, [connected]);
64+
65+
useEffect(() => () => {
66+
if (instance) {
67+
instance.removeListener('server log', handleServerLog);
68+
instance.removeListener('console output', handleConsoleOutput);
69+
}
70+
});
71+
5472
return (
5573
<div className={'text-xs font-mono relative'}>
5674
<SpinnerOverlay visible={!connected} large={true}/>

resources/scripts/plugins/Websocket.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ export class Websocket extends EventEmitter {
3939
this.socket.open();
4040
}
4141

42-
json (data: any) {
43-
this.socket.json(data);
44-
}
45-
4642
reconnect () {
4743
this.socket.reconnect();
4844
}
4945

50-
send (data: any) {
51-
this.socket.send(data);
46+
send (event: string, payload?: string | string[]) {
47+
this.socket.send(JSON.stringify({
48+
event, args: Array.isArray(payload) ? payload : [ payload ],
49+
}));
5250
}
5351
}

resources/scripts/state/models/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import getServer, { Server } from '@/api/server/getServer';
22
import { action, Action, thunk, Thunk } from 'easy-peasy';
33
import socket, { SocketState } from './socket';
44

5-
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'online';
5+
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running';
66

77
export interface ServerState {
88
data?: Server;

0 commit comments

Comments
 (0)