utf8 switch in server.js

This commit is contained in:
Jörg Breitbart
2019-05-13 01:40:54 +02:00
parent 0b50c06810
commit 67be8c36e7
2 changed files with 28 additions and 4 deletions
+2 -2
View File
@@ -154,8 +154,8 @@ function runRealTerminal(): void {
* To run it with UTF8 binary transport, swap comment on
* the lines below. (Must also be switched in server.js)
*/
term.loadAddon(new AttachAddon(socket));
//term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));
//term.loadAddon(new AttachAddon(socket));
term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));
term._initialized = true;
}
+26 -2
View File
@@ -3,6 +3,13 @@ var expressWs = require('express-ws');
var os = require('os');
var pty = require('node-pty');
/**
* Whether to use UTF8 binary transport.
* (Must also be switched in client.ts)
*/
const USE_BINARY_UTF8 = true;
function startServer() {
var app = express();
expressWs(app);
@@ -36,7 +43,8 @@ function startServer() {
cols: cols || 80,
rows: rows || 24,
cwd: process.env.PWD,
env: process.env
env: process.env,
encoding: USE_BINARY_UTF8 ? null : 'utf8'
});
console.log('Created terminal with PID: ' + term.pid);
@@ -65,6 +73,7 @@ function startServer() {
console.log('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);
// string message buffering
function buffer(socket, timeout) {
let s = '';
let sender = null;
@@ -79,7 +88,22 @@ function startServer() {
}
};
}
const send = buffer(ws, 5);
// binary message buffering
function bufferUtf8(socket, timeout) {
let buffer = [];
let sender = null;
return (data) => {
buffer.push(data);
if (!sender) {
sender = setTimeout(() => {
socket.send(Buffer.concat(buffer));
buffer = [];
sender = null;
}, timeout);
}
};
}
const send = USE_BINARY_UTF8 ? bufferUtf8(ws, 5) : buffer(ws, 5);
term.on('data', function(data) {
try {