RLG-Web: begin converting to WebSockets.
Use WebSockets for watching, if the browser supports it. Functionality is not complete yet.
This commit is contained in:
parent
e143ac7d20
commit
aee00a29d5
2 changed files with 88 additions and 1 deletions
33
rlgterm.js
33
rlgterm.js
|
|
@ -82,7 +82,9 @@ var session = {
|
|||
lname: null,
|
||||
lcred: null,
|
||||
/* Whether the game is being played or just watched. */
|
||||
playing: false
|
||||
playing: false,
|
||||
/* WebSocket for communication */
|
||||
sock: null
|
||||
};
|
||||
|
||||
/* The interval ID for checking the status of current games. */
|
||||
|
|
@ -655,6 +657,10 @@ function startgame(game) {
|
|||
function startwatching(gamenumber) {
|
||||
if (session.id != null)
|
||||
return;
|
||||
if (WebSocket) {
|
||||
wsWatch(gamenumber);
|
||||
return;
|
||||
}
|
||||
var wmsg = {"n": Number(gamenumber)};
|
||||
var req = new XMLHttpRequest();
|
||||
req.onerror = errHandler;
|
||||
|
|
@ -692,6 +698,27 @@ function makeWatcher(n) {
|
|||
return watcher;
|
||||
}
|
||||
|
||||
function wsWatch(gamenumber) {
|
||||
var sockurl = "ws://localhost:8080/watch/" + String(gamenumber);
|
||||
var ws = new WebSocket(sockurl);
|
||||
ws.onopen = function (event) {
|
||||
session.id = true;
|
||||
session.sock = ws;
|
||||
message("You are now watching game #" + gamenumber + ".");
|
||||
setmode("watch");
|
||||
};
|
||||
ws.onmessage = function (event) {
|
||||
var msgObject = JSON.parse(event.data);
|
||||
if (msgObject.t == 'd') {
|
||||
writeData(msgObject.d);
|
||||
}
|
||||
};
|
||||
ws.onclose = function (event) {
|
||||
session.sock = null;
|
||||
gameover();
|
||||
};
|
||||
}
|
||||
|
||||
function formreg(ev) {
|
||||
ev.preventDefault();
|
||||
if (session.id != null)
|
||||
|
|
@ -775,6 +802,10 @@ function logout() {
|
|||
function stop() {
|
||||
if (!session.id)
|
||||
return;
|
||||
if (session.sock) {
|
||||
session.sock.close();
|
||||
return;
|
||||
}
|
||||
var req = new XMLHttpRequest();
|
||||
req.onerror = errHandler;
|
||||
req.onreadystatechange = function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue