add websocket support, remote terminal as example

This commit is contained in:
lxsang
2016-11-12 17:39:11 +01:00
parent 725deacf9f
commit eb63cd0fa4
17 changed files with 6625 additions and 94 deletions

BIN
build/.DS_Store vendored

Binary file not shown.

BIN
build/htdocs/.DS_Store vendored

Binary file not shown.

View File

@ -194,7 +194,36 @@ var Terminal = (function () {
return TerminalConstructor
}());
var wtermobj;
var nest_callback;
var socket = new WebSocket("ws://127.0.0.1:9191/wterm?q=test");
//ws.binaryType = 'arraybuffer';
socket.onopen = function(){}
//{
// Web Socket is connected, send data using send()
// var msg = "Lorem Ipsum ";
// ws.send(msg);
//ws.send(array);
// alert("Message is sent...");
//};
socket.onmessage = function (e) {
if(wtermobj)
{
wtermobj.print(e.data);
var par = $("#wterm");
par.animate({
scrollTop: par.get(0).scrollHeight
}, 0);
wtermobj.input("antd> ",nest_callback);
}
};
socket.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
var wterm_config = {
name: 'wterm_layout',
panels: [
@ -210,25 +239,13 @@ var wterm_config = {
{
wtermobj = new Terminal();
wtermobj.setTextSize(11);
var nest_callback = function(data)
nest_callback = function(data)
{
if(data.length>0)
{
//send to server
var webtty = new EventSource('/wterm?cmd='+data);
webtty.onmessage = function (e) {
wtermobj.print(e.data);
var par = $("#wterm");
par.animate({
scrollTop: par.get(0).scrollHeight
}, 0);
};
webtty.onerror = function(e)
{
// finish the command
webtty.close();
wtermobj.input("antd> ",nest_callback);
}
//alert("sent " + data);
socket.send(data);
//alert("sent "+data);
}
else
{

View File

@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<title>term.js</title>
<style>
html {
background: #555;
}
h1 {
margin-bottom: 20px;
font: 20px/1.5 sans-serif;
}
.terminal {
float: left;
border: #000 solid 5px;
font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;
font-size: 11px;
color: #f0f0f0;
background: #000;
}
.terminal-cursor {
color: #000;
background: #f0f0f0;
}
</style>
<script src="term/term.js"></script>
<body>
<h1>Terminal</h1>
</body>
<script>
window.onload = function() {
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
var socket = new WebSocket("ws://127.0.0.1:9191/wterm?q=test");
var term = null;
socket.onopen = function(){
term = new Terminal({
cols: 80,
rows: 24,
useStyle: true,
screenKeys: true,
cursorBlink: false
});
term.on('data', function(data) {
socket.send(data);
});
term.on('title', function(title) {
document.title = title;
});
term.open(document.body);
term.write('\x1b[31mWelcome to term.js!\x1b[m\r\n');
};
socket.onmessage = function (e) {
if(term && e.data)
{
term.write(e.data.replaceAll("\n","\r\n"));
}
};
socket.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
window.onbeforeunload = function(e) {
if(socket) socket.close();
};
};
</script>
</html>

5977
build/htdocs/term/term.js Normal file

File diff suppressed because it is too large Load Diff