2012-02-06 22:00:24 +00:00
|
|
|
/*
|
2012-02-02 22:47:23 +00:00
|
|
|
* Copyright (c) 2012, Andy Janata
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
|
|
* and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-23 02:48:20 +00:00
|
|
|
/**
|
|
|
|
* AJAX long polling library.
|
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2011-12-23 02:48:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
cah.longpoll = {};
|
2012-03-13 04:05:19 +00:00
|
|
|
cah.longpoll.TIMEOUT = 45 * 1000;
|
2012-01-12 21:07:05 +00:00
|
|
|
// cah.longpoll.TIMEOUT = 30 * 1000;
|
2012-02-06 22:00:24 +00:00
|
|
|
|
2012-01-31 07:49:46 +00:00
|
|
|
/**
|
|
|
|
* Backoff when there was an error.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.INITIAL_BACKOFF = 500;
|
2012-02-06 22:00:24 +00:00
|
|
|
|
2012-01-31 07:49:46 +00:00
|
|
|
/**
|
|
|
|
* Backoff after a successful request.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
cah.longpoll.NORMAL_BACKOFF = 1;
|
2012-02-06 22:00:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current backkoff.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.Backoff = cah.longpoll.INITIAL_BACKOFF;
|
|
|
|
cah.longpoll.Resume = true;
|
|
|
|
cah.longpoll.ErrorCodeHandlers = {};
|
2011-12-25 03:37:45 +00:00
|
|
|
cah.longpoll.EventHandlers = {};
|
2011-12-23 02:48:20 +00:00
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Start a long polling operation.
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.longPoll = function() {
|
|
|
|
cah.log.debug("starting long poll");
|
|
|
|
$.ajax({
|
2012-03-13 07:42:07 +00:00
|
|
|
cache : false,
|
2011-12-23 02:48:20 +00:00
|
|
|
complete : cah.longpoll.complete,
|
|
|
|
error : cah.longpoll.error,
|
|
|
|
success : cah.longpoll.done,
|
|
|
|
timeout : cah.longpoll.TIMEOUT,
|
2012-01-31 07:49:46 +00:00
|
|
|
url : cah.LONGPOLL_URI,
|
2011-12-23 02:48:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Called when a long polling operation completes, success or failure. Will start another poll
|
|
|
|
* unless the previous one ended in a terminal failure (likely because the server restarted and we
|
|
|
|
* no longer have a session on it).
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.complete = function() {
|
|
|
|
if (cah.longpoll.Resume) {
|
|
|
|
setTimeout("cah.longpoll.longPoll()", cah.longpoll.Backoff);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Called when a long polling operation completes successfully. Dispatches the events to the
|
|
|
|
* appropriate handler.
|
|
|
|
*
|
|
|
|
* @param {Array}
|
|
|
|
* data_list A list of events to process.
|
|
|
|
*/
|
2012-01-07 00:43:40 +00:00
|
|
|
cah.longpoll.done = function(data_list) {
|
|
|
|
cah.log.debug("long poll done", data_list);
|
2011-12-23 02:48:20 +00:00
|
|
|
|
2012-01-07 00:49:43 +00:00
|
|
|
var data_list_work;
|
|
|
|
// we need to handle non-array data, too, so just make it look like an array
|
2012-01-17 00:28:21 +00:00
|
|
|
if (data_list[cah.$.LongPollResponse.TIMESTAMP] || data_list[cah.$.LongPollResponse.ERROR]) {
|
2012-01-07 00:49:43 +00:00
|
|
|
data_list_work = {};
|
|
|
|
data_list_work[0] = data_list;
|
|
|
|
} else {
|
|
|
|
data_list_work = data_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( var index in data_list_work) {
|
|
|
|
var data = data_list_work[index];
|
2012-01-17 00:28:21 +00:00
|
|
|
if (data[cah.$.LongPollResponse.ERROR]) {
|
2012-01-07 00:43:40 +00:00
|
|
|
// TODO cancel any timers or whatever we may have, and disable interface
|
|
|
|
// this probably should be done in the appropriate error code handler because we may not
|
|
|
|
// want to always be that extreme.
|
2012-01-17 00:28:21 +00:00
|
|
|
var errorCode = data[cah.$.LongPollResponse.ERROR_CODE];
|
|
|
|
if (cah.longpoll.ErrorCodeHandlers[errorCode]) {
|
|
|
|
cah.longpoll.ErrorCodeHandlers[errorCode](data);
|
2012-01-07 00:43:40 +00:00
|
|
|
} else {
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.log.error(cah.$.ErrorCode_msg[errorCode]);
|
2012-01-07 00:43:40 +00:00
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
} else {
|
2012-01-17 00:28:21 +00:00
|
|
|
var event = data[cah.$.LongPollResponse.EVENT];
|
|
|
|
if (cah.longpoll.EventHandlers[event]) {
|
|
|
|
cah.longpoll.EventHandlers[event](data);
|
2012-01-07 00:43:40 +00:00
|
|
|
} else {
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.log.error("Unhandled event " + event);
|
2012-01-07 00:43:40 +00:00
|
|
|
}
|
2011-12-25 03:37:45 +00:00
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset the backoff to normal when there's a successful operation
|
2012-01-31 07:49:46 +00:00
|
|
|
cah.longpoll.Backoff = cah.longpoll.NORMAL_BACKOFF;
|
2011-12-23 02:48:20 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Called when a long polling operation completes with an error.
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.error = function(jqXHR, textStatus, errorThrown) {
|
|
|
|
// TODO deal with this somehow
|
|
|
|
cah.log.debug(textStatus);
|
2012-01-31 07:49:46 +00:00
|
|
|
if (cah.longpoll.Backoff < cah.longpoll.INITIAL_BACKOFF) {
|
|
|
|
cah.longpoll.Backoff = cah.longpoll.INITIAL_BACKOFF;
|
|
|
|
} else {
|
|
|
|
cah.longpoll.Backoff *= 2;
|
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.log
|
|
|
|
.error("Error communicating with server. Will try again in " + (cah.longpoll.Backoff / 1000)
|
|
|
|
+ " second" + (cah.longpoll.Backoff != 1000 ? "s" : "") + ".");
|
|
|
|
};
|