comparison rlgterm.js @ 112:4f2b89e6fde2

RLG-Web: improvements to choices and status messaging. Server-side, have gamemux 'end' events include the name and game, so they can be sent to WebSockets connected to /status. This means a WebSocket client only needs to update its choice list when it gets a begin or end message with its own username. So it only needs to check /pstatus/<name> at those times and can stop polling.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 16 Jul 2012 08:23:51 -0700
parents f56fdfeed01a
children d7d7cdcba3b4
comparison
equal deleted inserted replaced
111:f56fdfeed01a 112:4f2b89e6fde2
531 statsock.close(); 531 statsock.close();
532 statsock = null; 532 statsock = null;
533 } 533 }
534 return; 534 return;
535 } 535 }
536 if (session.lcred) {
537 /* When starting the socket, the choices list might not be initialized. */
538 getchoices();
539 }
536 if (statsock) 540 if (statsock)
537 return; 541 return;
538 statsock = new WebSocket("ws://" + window.location.host + "/status"); 542 statsock = new WebSocket("ws://" + window.location.host + "/status");
539 statsock.onmessage = function (ev) { 543 statsock.onmessage = function (ev) {
540 var msg; 544 var msg;
542 msg = JSON.parse(ev.data); 546 msg = JSON.parse(ev.data);
543 } catch (e) { 547 } catch (e) {
544 if (e instanceof SyntaxError) 548 if (e instanceof SyntaxError)
545 return; 549 return;
546 } 550 }
547 if (msg.t != "t") { 551 if (msg.t == "t") {
548 return; 552 tableCurrent(msg.g);
549 } 553 }
550 tableCurrent(msg.g); 554 else if ((msg.t == "b" || msg.t == "e") && msg.p == session.lname) {
555 getchoices();
556 }
551 }; 557 };
552 statsock.onclose = function (ev) { 558 statsock.onclose = function (ev) {
553 statsock = null; 559 statsock = null;
554 } 560 }
555 } 561 }
556 562
557 function getcurrent(clear) { 563 function getcurrent(clear) {
564 if (WebSocket) {
565 return;
566 }
558 if (session.id || clear) { 567 if (session.id || clear) {
559 if (statInterval) { 568 if (statInterval) {
560 window.clearInterval(statInterval); 569 window.clearInterval(statInterval);
561 statInterval = null; 570 statInterval = null;
562 } 571 }
565 if (!statInterval) { 574 if (!statInterval) {
566 statInterval = window.setInterval(getcurrent, statDelta); 575 statInterval = window.setInterval(getcurrent, statDelta);
567 } 576 }
568 if (session.lcred) 577 if (session.lcred)
569 getchoices(); 578 getchoices();
570 if (WebSocket) {
571 return;
572 }
573 var req = new XMLHttpRequest(); 579 var req = new XMLHttpRequest();
574 req.onerror = errHandler; 580 req.onerror = errHandler;
575 req.onreadystatechange = function () { 581 req.onreadystatechange = function () {
576 if (req.readyState != 4 || req.status != 200) 582 if (req.readyState != 4 || req.status != 200)
577 return; 583 return;