Move credentials into the drivers.
The session id, or whether the session is currently active on the server, are better handled in the drivers than in termemu.js.
This commit is contained in:
parent
ceed48eee5
commit
553f07360c
3 changed files with 29 additions and 28 deletions
40
rlgterm.js
40
rlgterm.js
|
|
@ -71,6 +71,8 @@ var games = {
|
||||||
/* Login name and key */
|
/* Login name and key */
|
||||||
var lname = null;
|
var lname = null;
|
||||||
var lcred = null;
|
var lcred = null;
|
||||||
|
/* The session id assigned by the server. */
|
||||||
|
var sessid = null;
|
||||||
|
|
||||||
function writeData(hexstr) {
|
function writeData(hexstr) {
|
||||||
var codenum;
|
var codenum;
|
||||||
|
|
@ -211,10 +213,10 @@ function processMsg(msg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getData() {
|
function getData() {
|
||||||
if (termemu.sessid == null)
|
if (sessid == null)
|
||||||
return;
|
return;
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
var msg = JSON.stringify({"id": termemu.sessid, "t": "n"});
|
var msg = JSON.stringify({"id": sessid, "t": "n"});
|
||||||
datareq.onreadystatechange = function () {
|
datareq.onreadystatechange = function () {
|
||||||
if (datareq.readyState == 4 && datareq.status == 200) {
|
if (datareq.readyState == 4 && datareq.status == 200) {
|
||||||
var wasdata = processMsg(datareq.responseText);
|
var wasdata = processMsg(datareq.responseText);
|
||||||
|
|
@ -243,7 +245,7 @@ function postResponseHandler() {
|
||||||
|
|
||||||
function sendback(str) {
|
function sendback(str) {
|
||||||
/* For responding to terminal queries. */
|
/* For responding to terminal queries. */
|
||||||
var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": str};
|
var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": str};
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
datareq.onreadystatechange = postResponseHandler;
|
datareq.onreadystatechange = postResponseHandler;
|
||||||
datareq.open('POST', '/feed', true);
|
datareq.open('POST', '/feed', true);
|
||||||
|
|
@ -252,7 +254,7 @@ function sendback(str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendkey(ev) {
|
function sendkey(ev) {
|
||||||
if (termemu.sessid == null)
|
if (sessid == null)
|
||||||
return;
|
return;
|
||||||
var keynum = ev.keyCode;
|
var keynum = ev.keyCode;
|
||||||
var code;
|
var code;
|
||||||
|
|
@ -290,10 +292,10 @@ function sendkey(ev) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Isn't this check redundant?
|
// Isn't this check redundant?
|
||||||
if (termemu.sessid != null)
|
if (sessid != null)
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": code};
|
var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": code};
|
||||||
datareq.onreadystatechange = postResponseHandler;
|
datareq.onreadystatechange = postResponseHandler;
|
||||||
datareq.open('POST', '/feed', true);
|
datareq.open('POST', '/feed', true);
|
||||||
datareq.send(JSON.stringify(msgDict));
|
datareq.send(JSON.stringify(msgDict));
|
||||||
|
|
@ -305,7 +307,7 @@ var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
|
||||||
}
|
}
|
||||||
|
|
||||||
function vkey(c) {
|
function vkey(c) {
|
||||||
if (termemu.sessid == null)
|
if (sessid == null)
|
||||||
return;
|
return;
|
||||||
var keystr;
|
var keystr;
|
||||||
if (c.match(/^[a-z]$/)) {
|
if (c.match(/^[a-z]$/)) {
|
||||||
|
|
@ -343,7 +345,7 @@ function vkey(c) {
|
||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
var msgDict = {"id": termemu.sessid, "t": "d", "n": nsend++, "d": keystr};
|
var msgDict = {"id": sessid, "t": "d", "n": nsend++, "d": keystr};
|
||||||
datareq.onreadystatechange = postResponseHandler;
|
datareq.onreadystatechange = postResponseHandler;
|
||||||
datareq.open('POST', '/feed', true);
|
datareq.open('POST', '/feed', true);
|
||||||
datareq.send(JSON.stringify(msgDict));
|
datareq.send(JSON.stringify(msgDict));
|
||||||
|
|
@ -380,7 +382,7 @@ function togglectrl() {
|
||||||
|
|
||||||
function formlogin(ev) {
|
function formlogin(ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
if (termemu.sessid != null)
|
if (sessid != null)
|
||||||
return;
|
return;
|
||||||
var loginmsg = {};
|
var loginmsg = {};
|
||||||
loginmsg["name"] = document.getElementById("input_name").value;
|
loginmsg["name"] = document.getElementById("input_name").value;
|
||||||
|
|
@ -410,7 +412,7 @@ function formlogin(ev) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getcurrent() {
|
function getcurrent() {
|
||||||
if (termemu.sessid)
|
if (sessid)
|
||||||
return;
|
return;
|
||||||
var req = new XMLHttpRequest();
|
var req = new XMLHttpRequest();
|
||||||
req.onreadystatechange = function () {
|
req.onreadystatechange = function () {
|
||||||
|
|
@ -454,7 +456,7 @@ function getcurrent() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getchoices() {
|
function getchoices() {
|
||||||
if (termemu.sessid != null || !lcred)
|
if (sessid != null || !lcred)
|
||||||
return;
|
return;
|
||||||
var req = new XMLHttpRequest();
|
var req = new XMLHttpRequest();
|
||||||
req.onreadystatechange = function () {
|
req.onreadystatechange = function () {
|
||||||
|
|
@ -521,7 +523,7 @@ function makeStarter(gname) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function startgame(game) {
|
function startgame(game) {
|
||||||
if (termemu.sessid != null || !lcred)
|
if (sessid != null || !lcred)
|
||||||
return;
|
return;
|
||||||
var smsg = {};
|
var smsg = {};
|
||||||
smsg["key"] = lcred;
|
smsg["key"] = lcred;
|
||||||
|
|
@ -535,10 +537,10 @@ function startgame(game) {
|
||||||
var reply = JSON.parse(req.responseText);
|
var reply = JSON.parse(req.responseText);
|
||||||
if (reply.t == 's') {
|
if (reply.t == 's') {
|
||||||
/* Success */
|
/* Success */
|
||||||
termemu.sessid = reply.id;
|
sessid = reply.id;
|
||||||
termemu.resize(reply.h, reply.w);
|
termemu.resize(reply.h, reply.w);
|
||||||
setTitle("Playing as " + lname);
|
setTitle("Playing as " + lname);
|
||||||
debug(1, "Playing with id " + termemu.sessid);
|
debug(1, "Playing with id " + sessid);
|
||||||
setmode("play");
|
setmode("play");
|
||||||
getData();
|
getData();
|
||||||
}
|
}
|
||||||
|
|
@ -556,7 +558,7 @@ function startgame(game) {
|
||||||
|
|
||||||
function formreg(ev) {
|
function formreg(ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
if (termemu.sessid != null)
|
if (sessid != null)
|
||||||
return;
|
return;
|
||||||
var regmsg = {};
|
var regmsg = {};
|
||||||
regmsg["name"] = document.getElementById("regin_name").value;
|
regmsg["name"] = document.getElementById("regin_name").value;
|
||||||
|
|
@ -589,10 +591,10 @@ function formreg(ev) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function gameover() {
|
function gameover() {
|
||||||
if (termemu.sessid == null)
|
if (sessid == null)
|
||||||
return;
|
return;
|
||||||
/* TODO IFACE2 If the end was unexpected, tell player the game was saved. */
|
/* TODO IFACE2 If the end was unexpected, tell player the game was saved. */
|
||||||
termemu.sessid = null;
|
sessid = null;
|
||||||
ajaxstate.clear();
|
ajaxstate.clear();
|
||||||
setTitle("Game over.");
|
setTitle("Game over.");
|
||||||
nsend = 0;
|
nsend = 0;
|
||||||
|
|
@ -609,7 +611,7 @@ function logout() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop() {
|
function stop() {
|
||||||
if (!termemu.sessid)
|
if (!sessid)
|
||||||
return;
|
return;
|
||||||
var req = new XMLHttpRequest();
|
var req = new XMLHttpRequest();
|
||||||
req.onreadystatechange = function () {
|
req.onreadystatechange = function () {
|
||||||
|
|
@ -619,7 +621,7 @@ function stop() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
req.open('POST', '/feed', true);
|
req.open('POST', '/feed', true);
|
||||||
req.send(JSON.stringify({"id": termemu.sessid, "t": "q"}));
|
req.send(JSON.stringify({"id": sessid, "t": "q"}));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
15
shterm.js
15
shterm.js
|
|
@ -2,6 +2,7 @@
|
||||||
* is running a shell via the webtty.js server.
|
* is running a shell via the webtty.js server.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var isalive = false; // Whether the session is currently active.
|
||||||
var nsend = 0; // The number of the next packet to send.
|
var nsend = 0; // The number of the next packet to send.
|
||||||
var nrecv = 0; // The next packet expected.
|
var nrecv = 0; // The next packet expected.
|
||||||
var msgQ = []; // Queue for out-of-order messages.
|
var msgQ = []; // Queue for out-of-order messages.
|
||||||
|
|
@ -147,7 +148,7 @@ function processMsg(response) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getData() {
|
function getData() {
|
||||||
if (!termemu.alive)
|
if (!isalive)
|
||||||
return;
|
return;
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
datareq.onreadystatechange = function () {
|
datareq.onreadystatechange = function () {
|
||||||
|
|
@ -157,7 +158,7 @@ function getData() {
|
||||||
return;
|
return;
|
||||||
else if (response.t == "E") {
|
else if (response.t == "E") {
|
||||||
if (response.c == 1) {
|
if (response.c == 1) {
|
||||||
termemu.alive = false;
|
isalive = false;
|
||||||
debug(1, "Server error: " + response.s);
|
debug(1, "Server error: " + response.s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,7 +183,7 @@ function postResponseHandler() {
|
||||||
return;
|
return;
|
||||||
else if (response.t == "E") {
|
else if (response.t == "E") {
|
||||||
if (response.c == 1) {
|
if (response.c == 1) {
|
||||||
termemu.alive = false;
|
isalive = false;
|
||||||
debug(1, "Server error: " + response.s);
|
debug(1, "Server error: " + response.s);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
@ -245,7 +246,7 @@ function sendkey(ev) {
|
||||||
debug(1, "Ignoring keycode " + keynum);
|
debug(1, "Ignoring keycode " + keynum);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (termemu.alive)
|
if (isalive)
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
var formdata = {"t": "d", "n": nsend++, "d": code};
|
var formdata = {"t": "d", "n": nsend++, "d": code};
|
||||||
var datareq = new XMLHttpRequest();
|
var datareq = new XMLHttpRequest();
|
||||||
|
|
@ -333,7 +334,7 @@ function togglectrl() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(h, w) {
|
function login(h, w) {
|
||||||
if (termemu.alive)
|
if (isalive)
|
||||||
return;
|
return;
|
||||||
params = {"login": true, "h": h, "w": w};
|
params = {"login": true, "h": h, "w": w};
|
||||||
var req = new XMLHttpRequest();
|
var req = new XMLHttpRequest();
|
||||||
|
|
@ -343,7 +344,7 @@ function login(h, w) {
|
||||||
if (logindict.login) {
|
if (logindict.login) {
|
||||||
/* Success */
|
/* Success */
|
||||||
termemu.resize(logindict.h, logindict.w);
|
termemu.resize(logindict.h, logindict.w);
|
||||||
termemu.alive = true;
|
isalive = true;
|
||||||
nsend = 0;
|
nsend = 0;
|
||||||
nrecv = 0;
|
nrecv = 0;
|
||||||
setTitle("Logged in");
|
setTitle("Logged in");
|
||||||
|
|
@ -366,7 +367,7 @@ function stop() {
|
||||||
if (req.readyState == 4 && req.status == 200) {
|
if (req.readyState == 4 && req.status == 200) {
|
||||||
/* Figure out whether or not it worked. */
|
/* Figure out whether or not it worked. */
|
||||||
/* FIXME the server might respond with output. */
|
/* FIXME the server might respond with output. */
|
||||||
termemu.alive = false;
|
isalive = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -53,8 +53,6 @@ function Cursor(src) {
|
||||||
|
|
||||||
// An object representing the terminal emulator.
|
// An object representing the terminal emulator.
|
||||||
var termemu = {
|
var termemu = {
|
||||||
sessid: null, // Session key assigned by the server
|
|
||||||
alive: false,
|
|
||||||
/* Some elements of the page. */
|
/* Some elements of the page. */
|
||||||
inwrap: null, // A non-table div wrapping the screen
|
inwrap: null, // A non-table div wrapping the screen
|
||||||
view: null, // The div holding the terminal screen
|
view: null, // The div holding the terminal screen
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue