comparison rlgterm.js @ 100:3dbfdaf62623

RLG-Web: begin converting to WebSockets. Use WebSockets for watching, if the browser supports it. Functionality is not complete yet.
author John "Elwin" Edwards <elwin@sdf.org>
date Thu, 12 Jul 2012 22:16:15 -0700
parents 8a748eac7c11
children e59d68082664
comparison
equal deleted inserted replaced
99:2880cc4372bd 100:3dbfdaf62623
80 id: null, 80 id: null,
81 /* Login name and key */ 81 /* Login name and key */
82 lname: null, 82 lname: null,
83 lcred: null, 83 lcred: null,
84 /* Whether the game is being played or just watched. */ 84 /* Whether the game is being played or just watched. */
85 playing: false 85 playing: false,
86 /* WebSocket for communication */
87 sock: null
86 }; 88 };
87 89
88 /* The interval ID for checking the status of current games. */ 90 /* The interval ID for checking the status of current games. */
89 var statInterval = null; 91 var statInterval = null;
90 /* How frequently to check. */ 92 /* How frequently to check. */
653 } 655 }
654 656
655 function startwatching(gamenumber) { 657 function startwatching(gamenumber) {
656 if (session.id != null) 658 if (session.id != null)
657 return; 659 return;
660 if (WebSocket) {
661 wsWatch(gamenumber);
662 return;
663 }
658 var wmsg = {"n": Number(gamenumber)}; 664 var wmsg = {"n": Number(gamenumber)};
659 var req = new XMLHttpRequest(); 665 var req = new XMLHttpRequest();
660 req.onerror = errHandler; 666 req.onerror = errHandler;
661 req.onreadystatechange = function () { 667 req.onreadystatechange = function () {
662 if (req.readyState != 4 || req.status != 200) 668 if (req.readyState != 4 || req.status != 200)
690 startwatching(n); 696 startwatching(n);
691 } 697 }
692 return watcher; 698 return watcher;
693 } 699 }
694 700
701 function wsWatch(gamenumber) {
702 var sockurl = "ws://localhost:8080/watch/" + String(gamenumber);
703 var ws = new WebSocket(sockurl);
704 ws.onopen = function (event) {
705 session.id = true;
706 session.sock = ws;
707 message("You are now watching game #" + gamenumber + ".");
708 setmode("watch");
709 };
710 ws.onmessage = function (event) {
711 var msgObject = JSON.parse(event.data);
712 if (msgObject.t == 'd') {
713 writeData(msgObject.d);
714 }
715 };
716 ws.onclose = function (event) {
717 session.sock = null;
718 gameover();
719 };
720 }
721
695 function formreg(ev) { 722 function formreg(ev) {
696 ev.preventDefault(); 723 ev.preventDefault();
697 if (session.id != null) 724 if (session.id != null)
698 return; 725 return;
699 var regmsg = {}; 726 var regmsg = {};
773 } 800 }
774 801
775 function stop() { 802 function stop() {
776 if (!session.id) 803 if (!session.id)
777 return; 804 return;
805 if (session.sock) {
806 session.sock.close();
807 return;
808 }
778 var req = new XMLHttpRequest(); 809 var req = new XMLHttpRequest();
779 req.onerror = errHandler; 810 req.onerror = errHandler;
780 req.onreadystatechange = function () { 811 req.onreadystatechange = function () {
781 if (req.readyState == 4 && req.status == 200) { 812 if (req.readyState == 4 && req.status == 200) {
782 processMsg(req.responseText); 813 processMsg(req.responseText);