paco_sako_server/public/poll.html

74 lines
2.2 KiB
HTML

<html>
<head>
<meta charset="UTF-8">
<title>Polling Test</title>
</head>
<body>
<div id="current"></div>
<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
<script>
let gameId = '5b687c23df28945e';
let currentRequest = null;
function startPoll(fromTime) {
const thisRequest = currentRequest = $.ajax({
dataType: 'json',
contentType: 'application/json',
//url: `https://jessemcdonald.info/pacosako/api/meta/${gameId}/poll/${fromTime || 0}`,
url: `https://jessemcdonald.info/pacosako/api/games/poll/${fromTime || 0}`,
cache: false,
timeout: 0,
}).done((data, textStatus, jqXHR) => {
if (currentRequest === thisRequest) {
if (jqXHR.status == 204) {
startPoll(fromTime);
} else {
$('<p></p>').text(JSON.stringify(data)).appendTo('#current');
startPoll(data.modified);
}
}
}).fail((jqXHR, textStatus, errorThrown) => {
if (currentRequest === thisRequest) {
$('#current').empty().text(JSON.stringify({ textStatus, errorThrown }));
setTimeout(() => {
if (currentRequest === thisRequest) {
startPoll(0);
}
}, 3000);
}
});
}
function stopPoll() {
const request = currentRequest;
if (request !== null) {
currentRequest = null;
request.abort();
}
}
function doPost(data) {
stopPoll();
$.ajax({
dataType: 'json',
contentType: 'application/json',
url: `https://jessemcdonald.info/pacosako/api/posttest`,
method: 'POST',
cache: false,
data: JSON.stringify(data),
timeout: 5000,
}).done((responseData, textStatus, jqXHR) => {
$('#current').empty().text(JSON.stringify(responseData));
}).fail((jqXHR, textStatus, errorThrown) => {
$('#current').empty().text(JSON.stringify({ textStatus, errorThrown }));
});
}
$(() => {
let counter = 0;
$('<button>Start Polling</button>').appendTo('body').on('click', () => startPoll());
$('<button>Stop Polling</button>').appendTo('body').on('click', () => stopPoll());
$('<button>Do Post</button>').appendTo('body').on('click', () => doPost({ counter: ++counter }));
});
</script>
</body>
</html>
<!-- vim:set noexpandtab sw=2 ts=2: -->