comparison rlgterm.js @ 108:86a458080e80

RLG-Web client: add playing through WebSockets. Add client-side support for WebSocket play.
author John "Elwin" Edwards <elwin@sdf.org>
date Sun, 15 Jul 2012 18:18:53 -0700
parents dc1414faee19
children 67b393f10c2b
comparison
equal deleted inserted replaced
107:b64e31c5ec31 108:86a458080e80
316 else { 316 else {
317 debug(1, "Ignoring keycode " + keynum); 317 debug(1, "Ignoring keycode " + keynum);
318 return; 318 return;
319 } 319 }
320 ev.preventDefault(); 320 ev.preventDefault();
321 var datareq = new XMLHttpRequest(); 321 if (session.sock) {
322 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code}; 322 session.sock.send(JSON.stringify({"t": "d", "d": code}));
323 datareq.onerror = errHandler; 323 }
324 datareq.onreadystatechange = postResponseHandler; 324 else {
325 datareq.open('POST', '/feed', true); 325 var datareq = new XMLHttpRequest();
326 datareq.send(JSON.stringify(msgDict)); 326 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": code};
327 datareq.onerror = errHandler;
328 datareq.onreadystatechange = postResponseHandler;
329 datareq.open('POST', '/feed', true);
330 datareq.send(JSON.stringify(msgDict));
331 }
327 return; 332 return;
328 } 333 }
329 334
330 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c", 335 var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
331 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e" 336 ';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
374 else if (c in kpkeys) { 379 else if (c in kpkeys) {
375 keystr = kpkeys[c]; 380 keystr = kpkeys[c];
376 } 381 }
377 else 382 else
378 return; 383 return;
379 var datareq = new XMLHttpRequest(); 384 if (session.sock) {
380 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr}; 385 session.sock.send(JSON.stringify({"t": "d", "d": keystr}));
381 datareq.onerror = errHandler; 386 }
382 datareq.onreadystatechange = postResponseHandler; 387 else {
383 datareq.open('POST', '/feed', true); 388 var datareq = new XMLHttpRequest();
384 datareq.send(JSON.stringify(msgDict)); 389 var msgDict = {"id": session.id, "t": "d", "n": nsend++, "d": keystr};
390 datareq.onerror = errHandler;
391 datareq.onreadystatechange = postResponseHandler;
392 datareq.open('POST', '/feed', true);
393 datareq.send(JSON.stringify(msgDict));
394 }
385 return; 395 return;
386 } 396 }
387 397
388 function setup() { 398 function setup() {
389 keyHexCodes.init(); 399 keyHexCodes.init();
650 } 660 }
651 661
652 function startgame(game) { 662 function startgame(game) {
653 if (session.id != null || !session.lcred) 663 if (session.id != null || !session.lcred)
654 return; 664 return;
665 if (WebSocket) {
666 wsStart(game);
667 return;
668 }
655 var smsg = {}; 669 var smsg = {};
656 smsg["key"] = session.lcred; 670 smsg["key"] = session.lcred;
657 smsg["game"] = game.uname; 671 smsg["game"] = game.uname;
658 smsg["h"] = 24; 672 smsg["h"] = 24;
659 smsg["w"] = 80; 673 smsg["w"] = 80;
693 req.open('POST', '/play', true); 707 req.open('POST', '/play', true);
694 req.send(JSON.stringify(smsg)); 708 req.send(JSON.stringify(smsg));
695 return; 709 return;
696 } 710 }
697 711
712 function wsStart(game) {
713 var sockurl = "ws://" + window.location.host + "/play/" + game.uname;
714 sockurl += "?key=" + session.lcred + "&w=80&h=24";
715 ws = new WebSocket(sockurl);
716 ws.onopen = function (event) {
717 session.id = true;
718 session.playing = true;
719 session.sock = ws;
720 setmode("play");
721 };
722 ws.onmessage = function (event) {
723 var msgObject = JSON.parse(event.data);
724 if (msgObject.t == 's') {
725 termemu.resize(msgObject.h, msgObject.w);
726 message("You are now playing " + msgObject.g + ".");
727 }
728 else if (msgObject.t == 'd') {
729 writeData(msgObject.d);
730 }
731 };
732 ws.onclose = function (event) {
733 session.sock = null;
734 gameover();
735 };
736 }
737
698 function startwatching(gamenumber) { 738 function startwatching(gamenumber) {
699 if (session.id != null) 739 if (session.id != null)
700 return; 740 return;
701 if (WebSocket) { 741 if (WebSocket) {
702 wsWatch(gamenumber); 742 wsWatch(gamenumber);