comparison rlgterm.js @ 127:e54018b26ed8

RLG-Web client: store login key in DOM Storage. Keep the login key in sessionStorage. This lets the user navigate away and return without needing to log in again.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 27 Aug 2012 13:43:12 -0700
parents 54979d35611a
children a613380ffdc2
comparison
equal deleted inserted replaced
126:3e3824711791 127:e54018b26ed8
80 }; 80 };
81 81
82 var session = { 82 var session = {
83 /* The session id assigned by the server. */ 83 /* The session id assigned by the server. */
84 id: null, 84 id: null,
85 /* Login name and key */ 85 /* Login name and key are now in sessionStorage. */
86 lname: null,
87 lcred: null,
88 /* Whether the game is being played or just watched. */ 86 /* Whether the game is being played or just watched. */
89 playing: false, 87 playing: false,
90 /* WebSocket for communication */ 88 /* WebSocket for communication */
91 sock: null 89 sock: null
92 }; 90 };
400 } 398 }
401 399
402 function setup() { 400 function setup() {
403 keyHexCodes.init(); 401 keyHexCodes.init();
404 termemu.init("termwrap", 24, 80); 402 termemu.init("termwrap", 24, 80);
405 setmode("login"); 403 /* Is someone already logged in? */
404 if ("lcred" in sessionStorage) {
405 setmode("choose");
406 message("You are logged in as " + sessionStorage.getItem("lname") + ".");
407 }
408 else
409 setmode("login");
406 /* Set up the text size. */ 410 /* Set up the text size. */
407 var cssSize = termemu.view.style.fontSize; 411 var cssSize = termemu.view.style.fontSize;
408 var match = cssSize.match(/\d*/); 412 var match = cssSize.match(/\d*/);
409 if (!match) { 413 if (!match) {
410 return; 414 return;
461 if (req.readyState != 4 || req.status != 200) 465 if (req.readyState != 4 || req.status != 200)
462 return; 466 return;
463 var reply = JSON.parse(req.responseText); 467 var reply = JSON.parse(req.responseText);
464 if (reply.t == 'l') { 468 if (reply.t == 'l') {
465 /* Success */ 469 /* Success */
466 session.lcred = reply.k; 470 sessionStorage.setItem("lcred", reply.k);
467 session.lname = reply.u; 471 sessionStorage.setItem("lname", reply.u);
468 message("You are now logged in as " + reply.u + "."); 472 message("You are now logged in as " + reply.u + ".");
469 setmode("choose"); 473 setmode("choose");
470 } 474 }
471 else if (reply.t == 'E') { 475 else if (reply.t == 'E') {
472 var failmsg = "Logging in failed. "; 476 var failmsg = "Logging in failed. ";
535 statsock.close(); 539 statsock.close();
536 statsock = null; 540 statsock = null;
537 } 541 }
538 return; 542 return;
539 } 543 }
540 if (session.lcred) { 544 if ("lcred" in sessionStorage) {
541 /* When starting the socket, the choices list might not be initialized. */ 545 /* When starting the socket, the choices list might not be initialized. */
542 getchoices(); 546 getchoices();
543 } 547 }
544 if (statsock) 548 if (statsock)
545 return; 549 return;
553 return; 557 return;
554 } 558 }
555 if (msg.t == "t") { 559 if (msg.t == "t") {
556 tableCurrent(msg.g); 560 tableCurrent(msg.g);
557 } 561 }
558 else if ((msg.t == "b" || msg.t == "e") && msg.p == session.lname) { 562 else if ((msg.t == "b" || msg.t == "e") &&
563 msg.p == sessionStorage.getItem("lname")) {
559 getchoices(); 564 getchoices();
560 } 565 }
561 }; 566 };
562 statsock.onclose = function (ev) { 567 statsock.onclose = function (ev) {
563 statsock = null; 568 statsock = null;
576 return; 581 return;
577 } 582 }
578 if (!statInterval) { 583 if (!statInterval) {
579 statInterval = window.setInterval(getcurrent, statDelta); 584 statInterval = window.setInterval(getcurrent, statDelta);
580 } 585 }
581 if (session.lcred) 586 if ("lcred" in sessionStorage)
582 getchoices(); 587 getchoices();
583 var req = new XMLHttpRequest(); 588 var req = new XMLHttpRequest();
584 req.onerror = errHandler; 589 req.onerror = errHandler;
585 req.onreadystatechange = function () { 590 req.onreadystatechange = function () {
586 if (req.readyState != 4 || req.status != 200) 591 if (req.readyState != 4 || req.status != 200)
601 req.send(); 606 req.send();
602 return; 607 return;
603 } 608 }
604 609
605 function getchoices() { 610 function getchoices() {
606 if (session.id != null || !session.lcred) 611 if (session.id != null || !("lcred" in sessionStorage))
607 return; 612 return;
608 var req = new XMLHttpRequest(); 613 var req = new XMLHttpRequest();
609 req.onerror = errHandler; 614 req.onerror = errHandler;
610 req.onreadystatechange = function () { 615 req.onreadystatechange = function () {
611 if (req.readyState != 4 || req.status != 200) 616 if (req.readyState != 4 || req.status != 200)
615 reply = JSON.parse(req.responseText); 620 reply = JSON.parse(req.responseText);
616 } catch (e) { 621 } catch (e) {
617 if (e instanceof SyntaxError) 622 if (e instanceof SyntaxError)
618 return; 623 return;
619 } 624 }
620 if (!("name" in reply) || reply["name"] != session.lname || 625 if (!("name" in reply) || reply["name"] != sessionStorage.getItem("lname")
621 !("stat" in reply)) 626 || !("stat" in reply))
622 return; 627 return;
623 var optdiv = document.getElementById("opttable"); 628 var optdiv = document.getElementById("opttable");
624 /* Don't remove the first child, it's the header. */ 629 /* Don't remove the first child, it's the header. */
625 while (optdiv.childNodes.length > 1) 630 while (optdiv.childNodes.length > 1)
626 optdiv.removeChild(optdiv.childNodes[1]); 631 optdiv.removeChild(optdiv.childNodes[1]);
654 rowdiv.appendChild(gamediv); 659 rowdiv.appendChild(gamediv);
655 rowdiv.appendChild(actdiv); 660 rowdiv.appendChild(actdiv);
656 optdiv.appendChild(rowdiv); 661 optdiv.appendChild(rowdiv);
657 } 662 }
658 }; 663 };
659 req.open('GET', '/pstatus/' + session.lname, true); 664 req.open('GET', '/pstatus/' + sessionStorage.getItem("lname"), true);
660 req.send(); 665 req.send();
661 return; 666 return;
662 } 667 }
663 668
664 /* This can't be in the loop in getchoices(), or the closure's scope will 669 /* This can't be in the loop in getchoices(), or the closure's scope will
674 } 679 }
675 return starter; 680 return starter;
676 } 681 }
677 682
678 function startgame(game) { 683 function startgame(game) {
679 if (session.id != null || !session.lcred) 684 if (session.id != null || !("lcred" in sessionStorage))
680 return; 685 return;
681 if (window.WebSocket) { 686 if (window.WebSocket) {
682 wsStart(game); 687 wsStart(game);
683 return; 688 return;
684 } 689 }
685 var smsg = {}; 690 var smsg = {};
686 smsg["key"] = session.lcred; 691 smsg["key"] = sessionStorage.getItem("lcred");
687 smsg["game"] = game.uname; 692 smsg["game"] = game.uname;
688 smsg["h"] = 24; 693 smsg["h"] = 24;
689 smsg["w"] = 80; 694 smsg["w"] = 80;
690 var req = new XMLHttpRequest(); 695 var req = new XMLHttpRequest();
691 req.onerror = errHandler; 696 req.onerror = errHandler;
740 } 745 }
741 return stopper; 746 return stopper;
742 } 747 }
743 748
744 function stopgame(game) { 749 function stopgame(game) {
745 if (!session.lcred) 750 if (!("lcred" in sessionStorage))
746 return; 751 return;
747 var stopmsg = {"key": session.lcred, "g": game.uname}; 752 var stopmsg = {"key": sessionStorage.getItem("lcred"), "g": game.uname};
748 var req = new XMLHttpRequest(); 753 var req = new XMLHttpRequest();
749 req.onerror = errHandler; 754 req.onerror = errHandler;
750 req.onreadystatechange = function () { 755 req.onreadystatechange = function () {
751 if (req.readyState != 4 || req.status != 200) 756 if (req.readyState != 4 || req.status != 200)
752 return; 757 return;
769 return; 774 return;
770 } 775 }
771 776
772 function wsStart(game) { 777 function wsStart(game) {
773 var sockurl = "ws://" + window.location.host + "/play/" + game.uname; 778 var sockurl = "ws://" + window.location.host + "/play/" + game.uname;
774 sockurl += "?key=" + session.lcred + "&w=80&h=24"; 779 sockurl += "?key=" + sessionStorage.getItem("lcred") + "&w=80&h=24";
775 ws = new WebSocket(sockurl); 780 ws = new WebSocket(sockurl);
776 ws.onopen = function (event) { 781 ws.onopen = function (event) {
777 session.id = true; 782 session.id = true;
778 session.playing = true; 783 session.playing = true;
779 session.sock = ws; 784 session.sock = ws;
882 return; 887 return;
883 var reply = JSON.parse(req.responseText); 888 var reply = JSON.parse(req.responseText);
884 if (reply.t == 'r') { 889 if (reply.t == 'r') {
885 /* Success */ 890 /* Success */
886 message("Welcome " + reply.u + ", you are now registered."); 891 message("Welcome " + reply.u + ", you are now registered.");
887 session.lcred = reply.k; 892 sessionStorage.setItem("lcred", reply.k);
888 session.lname = reply.u; 893 sessionStorage.setItem("lname", reply.u);
889 message("You are now logged in as " + reply.u + "."); 894 message("You are now logged in as " + reply.u + ".");
890 setmode("choose"); 895 setmode("choose");
891 } 896 }
892 else if (reply.t == 'E') { 897 else if (reply.t == 'E') {
893 var failmsg = "Registration failed."; 898 var failmsg = "Registration failed.";
932 ajaxstate.clear(); 937 ajaxstate.clear();
933 termemu.toNormBuf(); 938 termemu.toNormBuf();
934 nsend = 0; 939 nsend = 0;
935 nrecv = 0; 940 nrecv = 0;
936 msgQ = []; 941 msgQ = [];
937 if (session.lcred != null) 942 if ("lcred" in sessionStorage)
938 setmode("choose"); 943 setmode("choose");
939 else 944 else
940 setmode("login"); 945 setmode("login");
941 return; 946 return;
942 } 947 }
943 948
944 function logout() { 949 function logout() {
945 session.lcred = null; 950 sessionStorage.removeItem("lcred");
946 session.lname = null; 951 sessionStorage.removeItem("lname");
947 setmode("login"); 952 setmode("login");
948 } 953 }
949 954
950 function stop() { 955 function stop() {
951 if (!session.id) 956 if (!session.id)