Last active
April 20, 2022 11:47
-
-
Save steinwaywhw/9920493 to your computer and use it in GitHub Desktop.
A minimal term.js server/client demo. To be used with node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var client = {}; | |
client.run = function (options) { | |
options = options || {}; | |
var socket = io.connect(options.remote || "http://localhost:8080"); | |
socket.on('connect', function() { | |
var term = new Terminal({ | |
cols: 120, | |
rows: 60, | |
useStyle: true, | |
screenKeys: true | |
}); | |
term.on('data', function(data) { | |
socket.emit('data', data); | |
}); | |
socket.on('data', function(data) { | |
term.write(data); | |
}); | |
term.open(options.parent || document.body); | |
term.write('WELCOME!\r\n'); | |
socket.on('disconnect', function() { | |
term.destroy(); | |
}); | |
// for displaying the first command line | |
socket.emit('data', '\n'); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script> | |
<script src="term.js"></script> | |
<script src="client.js"></script> | |
<style type="text/css"> | |
.terminal { | |
font-family: "DejaVu Sans Mono", "Liberation Mono", monospace; | |
font-size: 11px; | |
color: rgb(240, 240, 240); | |
background: #383734 !important; | |
padding: 3px; | |
border: none !important; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="terminal"></div> | |
<script> | |
var e = document.getElementById("terminal"); | |
client.run({ | |
parent: e, | |
remote: "http://localhost:8080/" | |
}) | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var server = require('./server.js'); | |
server.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var server = {} | |
var http = require('http'); | |
var express = require('express'); | |
var io = require('socket.io'); | |
var pty = require('pty.js'); | |
var terminal = require('term.js'); | |
var socket; | |
var term; | |
var buff = []; | |
server.run = function(options) { | |
// create shell process | |
term = pty.fork( | |
process.env.SHELL || 'sh', | |
[], | |
{ | |
name: require('fs').existsSync('/usr/share/terminfo/x/xterm-256color') | |
? 'xterm-256color' | |
: 'xterm', | |
cols: 80, | |
rows: 24, | |
cwd: process.env.HOME | |
} | |
); | |
// store term's output into buffer or emit through socket | |
term.on('data', function(data) { | |
return !socket ? buff.push(data) : socket.emit('data', data); | |
}); | |
console.log('Created shell with pty master/slave pair (master: %d, pid: %d)', term.fd, term.pid); | |
var app = express(); | |
var server = http.createServer(app); | |
// let term.js handle req/res | |
app.use(terminal.middleware()); | |
// let server listen on the port | |
options = options || {}; | |
server.listen(options.port || 8080); | |
// let socket.io handle sockets | |
io = io.listen(server, {log: false}); | |
io.sockets.on('connection', function(s) { | |
// when connect, store the socket | |
socket = s; | |
// handle incoming data (client -> server) | |
socket.on('data', function(data) { | |
term.write(data); | |
}); | |
// handle connection lost | |
socket.on('disconnect', function() { | |
socket = null; | |
}); | |
// send buffer data to client | |
while (buff.length) { | |
socket.emit('data', buff.shift()); | |
}; | |
}); | |
}; | |
//server.run({port: 8080}); | |
// export server object | |
module.exports = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bhsingh1108, look at
node-pty
. Supports both windows and linux. I assume it's the same tool.