-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
40 lines (30 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// index.js
import { TerminalUI } from "./TerminalUI";
import io from "socket.io-client";
// IMPORTANT: Make sure you replace this address with your server address.
const serverAddress = "http://localhost:8080";
//Server sandbox available at https://codesandbox.io/s/web-terminal-tutorial-server-g2ihu
function connectToSocket(serverAddress) {
return new Promise(res => {
const socket = io(serverAddress);
res(socket);
});
}
function startTerminal(container, socket) {
// Create an xterm.js instance (TerminalUI class is a wrapper with some utils. Check that file for info.)
const terminal = new TerminalUI(socket);
// Attach created terminal to a DOM element.
terminal.attachTo(container);
// When terminal attached to DOM, start listening for input, output events.
// Check TerminalUI startListening() function for details.
terminal.startListening();
}
function start() {
const container = document.getElementById("terminal-container");
// Connect to socket and when it is available, start terminal.
connectToSocket(serverAddress).then(socket => {
startTerminal(container, socket);
});
}
// Better to start on DOMContentLoaded. So, we know terminal-container is loaded
start();