It now works better!

This commit is contained in:
BuildTools 2019-09-03 15:31:27 +02:00
parent 803f76ac05
commit 2f18843893
4 changed files with 24 additions and 18 deletions

View File

@ -6,39 +6,33 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>webssh_plus</title> <title>webssh_plus</title>
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<link rel="stylesheet" href="node_modules/xterm/dist/xterm.css" />
<script src="node_modules/xterm/dist/xterm.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style> <style>
body { body {
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
div.terminal {
font-family: monospace;
color: white;
background: black;
}
</style> </style>
</head> </head>
<body> <body>
<div class="terminal"> <div id="terminal">
<!-- ! Here will be the terminal stuff --> <!-- ! Here will be the terminal stuff -->
</div> </div>
<script> <script>
var term = new Terminal();
term.open(document.getElementById('terminal'))
// Connect to the socket.io server // Connect to the socket.io server
var socket = io.connect('http://localhost:9774'); var socket = io.connect('http://localhost:9774');
// Wait for data from the server // Wait for data from the server
socket.on('output', function (data) { socket.on('output', function (data) {
// Insert some line breaks where they belong term.write(data);
data = data.replace("n", "");
data = data.replace("r", "");
// Append the data to our terminal
$('.terminal').append(data);
}); });
// Listen for user input and pass it to the server // Listen for user input and pass it to the server
$(document).on("keypress",function(e){ term.on('data', function(data) {
var char = String.fromCharCode(e.which); console.log(data);
socket.emit("input", char); socket.emit('input', data);
}); });
</script> </script>
</body> </body>

10
main.js
View File

@ -6,18 +6,24 @@ var http = require('http');
var fs = require('fs'); var fs = require('fs');
var pty = require('node-pty'); var pty = require('node-pty');
// Set port
var port = 9774;
// Set up express server // Set up express server
let app = express(); let app = express();
// Set HTTP server root folder
app.use("/", express.static("./")); app.use("/", express.static("./"));
// Creating an HTTP server // Creating an HTTP server
var server = http.createServer(app).listen(9774); var server = http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);
var io = require('socket.io')(server); var io = require('socket.io')(server);
// When a new socket connects // When a new socket connects
io.on('connection', function (socket) { io.on('connection', function (socket) {
console.log('Client connect');
// Create terminal // Create terminal
var term = pty.spawn('sh', [], { var term = pty.spawn('sh', [], {
name: 'xterm-color', name: 'xterm-color',
@ -37,6 +43,6 @@ io.on('connection', function (socket) {
// When socket disconnects, destroy the terminal // When socket disconnects, destroy the terminal
socket.on("disconnect", function () { socket.on("disconnect", function () {
term.destroy(); term.destroy();
console.log("bye"); console.log("Client disconnect");
}); });
}); });

5
package-lock.json generated
View File

@ -660,6 +660,11 @@
"resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz", "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz",
"integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4=" "integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4="
}, },
"xterm": {
"version": "3.14.5",
"resolved": "https://registry.npmjs.org/xterm/-/xterm-3.14.5.tgz",
"integrity": "sha512-DVmQ8jlEtL+WbBKUZuMxHMBgK/yeIZwkXB81bH+MGaKKnJGYwA+770hzhXPfwEIokK9On9YIFPRleVp/5G7z9g=="
},
"yeast": { "yeast": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz", "resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz",

View File

@ -19,6 +19,7 @@
"dependencies": { "dependencies": {
"express": "^4.17.1", "express": "^4.17.1",
"node-pty": "^0.8.1", "node-pty": "^0.8.1",
"socket.io": "^2.2.0" "socket.io": "^2.2.0",
"xterm": "^3.14.5"
} }
} }