2011-12-17 00:39:52 +00:00
|
|
|
var cah = {};
|
|
|
|
cah.ajax = {};
|
|
|
|
cah.ajax.ErrorHandlers = {};
|
|
|
|
cah.ajax.SuccessHandlers = {};
|
|
|
|
|
2011-12-22 22:19:59 +00:00
|
|
|
cah.DEBUG = true;
|
|
|
|
cah.LONG_POLL_TIMEOUT = 2 * 60 * 1000;
|
|
|
|
|
2011-12-17 00:39:52 +00:00
|
|
|
cah.ajax.ErrorHandlers.register = ajax_register_error;
|
|
|
|
cah.ajax.ErrorHandlers.firstload = ajax_firstload_error;
|
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers.register = ajax_register_success;
|
|
|
|
cah.ajax.SuccessHandlers.firstload = ajax_firstload_success;
|
|
|
|
|
|
|
|
var nickname;
|
|
|
|
var serial = 0;
|
|
|
|
// TODO run a timer to see if we have more than X pending requests and delay further ones until
|
|
|
|
// we get results
|
|
|
|
var pendingRequests = {};
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
$.ajaxSetup({
|
|
|
|
cache : false,
|
|
|
|
error : ajaxError,
|
|
|
|
success : ajaxDone,
|
2011-12-22 22:19:59 +00:00
|
|
|
timeout : cah.DEBUG ? undefined : 10 * 1000, // 10 second timeout for normal requests
|
|
|
|
// timeout : 1, // 10 second timeout for normal requests
|
2011-12-17 00:39:52 +00:00
|
|
|
type : 'POST',
|
|
|
|
url : '/cah/AjaxServlet'
|
|
|
|
});
|
|
|
|
|
|
|
|
ajaxRequest("firstload", {});
|
|
|
|
|
|
|
|
// TODO see if we have a stored nickname somewhere
|
|
|
|
$("#nicknameconfirm").click(nicknameconfirm_click);
|
|
|
|
$("#nickbox").keyup(nickbox_keyup);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
function nickbox_keyup(e) {
|
|
|
|
if (e.which == 13) {
|
|
|
|
$("#nicknameconfirm").click();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function nicknameconfirm_click(e) {
|
|
|
|
nickname = $.trim($("#nickname").val());
|
|
|
|
ajaxRequest("register", {
|
|
|
|
nickname : nickname
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLog(text) {
|
|
|
|
$("#log").append("[" + new Date().toLocaleTimeString() + "] " + text + "<br/>");
|
|
|
|
$("#log").prop("scrollTop", $("#log").prop("scrollHeight"));
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:19:59 +00:00
|
|
|
function addLogError(text) {
|
|
|
|
addLog("<span class='error'>Error: " + text + "</span>");
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLogDebug(text) {
|
|
|
|
if (cah.DEBUG) {
|
|
|
|
addLog("<span class='debug'>DEBUG: " + text + "</span>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLogDebugObject(text, obj) {
|
|
|
|
if (cah.DEBUG) {
|
|
|
|
if (JSON && JSON.stringify) {
|
|
|
|
addLogDebug(text + ": " + JSON.stringify(obj));
|
|
|
|
} else {
|
|
|
|
addLogDebug(text + ": TODO: debug log without JSON.stringify()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:39:52 +00:00
|
|
|
/**
|
|
|
|
* Send an ajax request to the server, and store that the request was sent so we know when it gets
|
|
|
|
* responded to.
|
|
|
|
*
|
2011-12-22 22:19:59 +00:00
|
|
|
* This should be used for data sent to the server, not long-polling.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* op Operation code for the request.
|
|
|
|
* @param {object}
|
|
|
|
* data Parameter map to send for the request.
|
|
|
|
* @param {?function(jqXHR,textStatus,errorThrown)}
|
|
|
|
* [opt_errback] Optional error callback.
|
2011-12-17 00:39:52 +00:00
|
|
|
*/
|
2011-12-22 22:19:59 +00:00
|
|
|
function ajaxRequest(op, data, opt_errback) {
|
2011-12-17 00:39:52 +00:00
|
|
|
data.op = op;
|
|
|
|
data.serial = serial++;
|
2011-12-22 22:19:59 +00:00
|
|
|
var jqXHR = $.ajax({
|
2011-12-17 00:39:52 +00:00
|
|
|
data : data
|
|
|
|
});
|
|
|
|
pendingRequests[data.serial] = data;
|
2011-12-22 22:19:59 +00:00
|
|
|
addLogDebugObject("ajax req", data);
|
|
|
|
if (opt_errback) {
|
|
|
|
jqXHR.fail(opt_errback);
|
|
|
|
}
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ajaxError(jqXHR, textStatus, errorThrown) {
|
|
|
|
// TODO deal with this somehow
|
|
|
|
// and figure out which request it was so we can remove it from pending
|
2011-12-22 22:19:59 +00:00
|
|
|
debugger;
|
|
|
|
addLogError(textStatus);
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ajaxDone(data) {
|
2011-12-22 22:19:59 +00:00
|
|
|
addLogDebugObject("ajax done", data);
|
2011-12-17 00:39:52 +00:00
|
|
|
if (data['error']) {
|
|
|
|
// TODO cancel any timers or whatever we may have, and disable interface
|
|
|
|
var req = pendingRequests[data.serial];
|
|
|
|
if (req && cah.ajax.ErrorHandlers[req.op]) {
|
|
|
|
cah.ajax.ErrorHandlers[req.op](data);
|
|
|
|
} else {
|
2011-12-22 22:19:59 +00:00
|
|
|
addLogError(data.error_message);
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var req = pendingRequests[data.serial];
|
|
|
|
if (req && cah.ajax.SuccessHandlers[req.op]) {
|
|
|
|
cah.ajax.SuccessHandlers[req.op](data);
|
|
|
|
} else if (req) {
|
2011-12-22 22:19:59 +00:00
|
|
|
addLogError("Unhandled response for op " + req.op);
|
2011-12-17 00:39:52 +00:00
|
|
|
} else {
|
2011-12-22 22:19:59 +00:00
|
|
|
addLogError("Unknown response for serial " + data.serial);
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.serial >= 0 && pendingRequests[data.serial]) {
|
|
|
|
delete pendingRequests[data.serial];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ajax_register_success(data) {
|
|
|
|
nickname = data['nickname'];
|
|
|
|
addLog("You are connected as " + nickname);
|
|
|
|
$("#nickbox").hide();
|
|
|
|
$("#canvass").show();
|
2011-12-22 22:19:59 +00:00
|
|
|
|
|
|
|
after_registered();
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ajax_register_error(data) {
|
|
|
|
$("#nickbox").append("<span class='error'>" + data.error_message + "</span>");
|
|
|
|
$("#nickname").focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function ajax_firstload_success(data) {
|
|
|
|
if (data.in_progress) {
|
|
|
|
// TODO reload data. see what 'next' is and go from there.
|
|
|
|
// for now just load the nickname
|
|
|
|
nickname = data['nickname'];
|
|
|
|
addLog("You have reconnected as " + nickname);
|
|
|
|
$("#nickbox").hide();
|
|
|
|
$("#canvass").show();
|
2011-12-22 22:19:59 +00:00
|
|
|
after_registered();
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ajax_firstload_error(data) {
|
2011-12-22 22:19:59 +00:00
|
|
|
// TODO dunno what to do here
|
|
|
|
}
|
2011-12-17 00:39:52 +00:00
|
|
|
|
2011-12-22 22:19:59 +00:00
|
|
|
/**
|
|
|
|
* This should only be called after we have a valid registration with the server, as we start doing
|
|
|
|
* long polling here.
|
|
|
|
*/
|
|
|
|
function after_registered() {
|
|
|
|
addLogDebug("done registering");
|
|
|
|
long_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
function long_poll() {
|
|
|
|
addLogDebug("starting long poll");
|
|
|
|
$.ajax({
|
|
|
|
complete : long_poll,
|
|
|
|
error : long_poll_error,
|
|
|
|
success : long_poll_done,
|
|
|
|
timeout : cah.LONG_POLL_TIMEOUT,
|
|
|
|
url : '/cah/LongPollServlet',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function long_poll_done(data) {
|
|
|
|
addLogDebugObject("long poll done", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function long_poll_error(jqXHR, textStatus, errorThrown) {
|
|
|
|
// TODO deal with this somehow
|
|
|
|
debugger;
|
|
|
|
addLogError(textStatus);
|
2011-12-17 00:39:52 +00:00
|
|
|
}
|