Browse Source

Merge pull request #34 from jarrettgilliam/master

Use socket.io instead of WebSockets
pull/41/head
Krishna Srinivas 9 years ago
parent
commit
b7e9179454
  1. 47
      app.js
  2. 2
      package.json
  3. 1
      public/index.html
  4. 1
      public/wetty/index.html
  5. 54
      public/wetty/wetty.js

47
app.js

@ -2,7 +2,7 @@ var express = require('express');
var http = require('http'); var http = require('http');
var https = require('https'); var https = require('https');
var path = require('path'); var path = require('path');
var ws = require('websocket').server; var server = require('socket.io');
var pty = require('pty.js'); var pty = require('pty.js');
var fs = require('fs'); var fs = require('fs');
@ -90,27 +90,18 @@ if (runhttps) {
}); });
} }
var wss = new ws({ var io = server(httpserv,{path: '/wetty/socket.io'});
httpServer: httpserv io.on('connection', function(socket){
});
wss.on('request', function(request) {
var term;
var sshuser = ''; var sshuser = '';
var conn = request.accept('wetty', request.origin); var request = socket.request;
console.log((new Date()) + ' Connection accepted.'); console.log((new Date()) + ' Connection accepted.');
if (request.resource.match('^/wetty/ssh/')) { if (match = request.headers.referer.match('/wetty/ssh/.+$')) {
sshuser = request.resource; sshuser = match[0].replace('/wetty/ssh/', '') + '@';
sshuser = sshuser.replace('/wetty/ssh/', '');
}
if (sshuser) {
sshuser = sshuser + '@';
} else if (globalsshuser) { } else if (globalsshuser) {
sshuser = globalsshuser + '@'; sshuser = globalsshuser + '@';
} }
conn.on('message', function(msg) {
var data = JSON.parse(msg.utf8Data); var term;
if (!term) {
if (process.getuid() == 0) { if (process.getuid() == 0) {
term = pty.spawn('/bin/login', [], { term = pty.spawn('/bin/login', [], {
name: 'xterm-256color', name: 'xterm-256color',
@ -126,26 +117,18 @@ wss.on('request', function(request) {
} }
console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser) console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser)
term.on('data', function(data) { term.on('data', function(data) {
conn.send(JSON.stringify({ socket.emit('output', data);
data: data
}));
}); });
term.on('exit', function(code) { term.on('exit', function(code) {
console.log((new Date()) + " PID=" + term.pid + " ENDED") console.log((new Date()) + " PID=" + term.pid + " ENDED")
}) });
} socket.on('resize', function(data) {
if (!data)
return;
if (data.rowcol) {
term.resize(data.col, data.row); term.resize(data.col, data.row);
} else if (data.data) {
term.write(data.data);
}
}); });
conn.on('error', function() { socket.on('input', function(data) {
term.end(); term.write(data);
}); });
conn.on('close', function() { socket.on('disconnect', function() {
term.end(); term.end();
}) });
}) })

2
package.json

@ -3,7 +3,7 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"express": "3.5.1", "express": "3.5.1",
"websocket": "^1.0", "socket.io": "^1.3.7",
"pty.js": "^0.2.7-1", "pty.js": "^0.2.7-1",
"optimist": "^0.6" "optimist": "^0.6"
}, },

1
public/index.html

@ -5,6 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Wetty - The WebTTY Terminal Emulator</title> <title>Wetty - The WebTTY Terminal Emulator</title>
<script src="/wetty/hterm_all.js"></script> <script src="/wetty/hterm_all.js"></script>
<script src="/wetty/socket.io/socket.io.js"></script>
<script src="/wetty/wetty.js"></script> <script src="/wetty/wetty.js"></script>
<style> <style>
html, html,

1
public/wetty/index.html

@ -5,6 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Wetty - The WebTTY Terminal Emulator</title> <title>Wetty - The WebTTY Terminal Emulator</title>
<script src="/wetty/hterm_all.js"></script> <script src="/wetty/hterm_all.js"></script>
<script src="/wetty/socket.io/socket.io.js"></script>
<script src="/wetty/wetty.js"></script> <script src="/wetty/wetty.js"></script>
<style> <style>
html, html,

54
public/wetty/wetty.js

@ -1,5 +1,6 @@
var term; var term;
var ws; var socket = io(location.origin, {path: '/wetty/socket.io'})
var buf = '';
function Wetty(argv) { function Wetty(argv) {
this.argv_ = argv; this.argv_ = argv;
@ -16,23 +17,16 @@ Wetty.prototype.run = function() {
} }
Wetty.prototype.sendString_ = function(str) { Wetty.prototype.sendString_ = function(str) {
ws.send(JSON.stringify({ socket.emit('input', str);
data: str
}));
}; };
Wetty.prototype.onTerminalResize = function(col, row) { Wetty.prototype.onTerminalResize = function(col, row) {
if (ws) socket.emit('resize', { col: col, row: row });
ws.send(JSON.stringify({
rowcol: true,
col: col,
row: row
}));
}; };
ws = new WebSocket(((window.location.protocol === 'https:') ? 'wss://' : 'ws://') + window.location.host + window.location.pathname, 'wetty'); socket.on('connect', function() {
ws.onopen = function() {
lib.init(function() { lib.init(function() {
hterm.defaultStorage = new lib.Storage.Local();
term = new hterm.Terminal(); term = new hterm.Terminal();
window.term = term; window.term = term;
term.decorate(document.getElementById('terminal')); term.decorate(document.getElementById('terminal'));
@ -44,23 +38,27 @@ ws.onopen = function() {
term.prefs_.set('use-default-window-copy', true); term.prefs_.set('use-default-window-copy', true);
term.runCommandClass(Wetty, document.location.hash.substr(1)); term.runCommandClass(Wetty, document.location.hash.substr(1));
ws.send(JSON.stringify({ socket.emit('resize', {
rowcol: true,
col: term.screenSize.width, col: term.screenSize.width,
row: term.screenSize.height row: term.screenSize.height
}));
}); });
}
ws.onmessage = function(msg) { if (buf && buf != '')
if (!msg || !msg.data) {
term.io.writeUTF16(buf);
buf = '';
}
});
});
socket.on('output', function(data) {
if (!term) {
buf += data;
return; return;
var data = JSON.parse(msg.data); }
if (term) term.io.writeUTF16(data);
term.io.writeUTF16(data.data); });
}
ws.onerror = function(e) { socket.on('disconnect', function() {
console.log("WebSocket connection error"); console.log("Socket.io connection closed");
} });
ws.onclose = function() {
console.log("WebSocket connection closed");
}

Loading…
Cancel
Save