var getMetaData = function (name) {
	return $('meta[name="'+name+'"]').attr('content');
}

var eventSource = undefined;

function handleSSEError(event) {
	console.log("SSE Error Occured");
	event.target.close();
	eventSource = getSSE();
}

function fitAddedListener(event) {
	var data = JSON.parse(event.data);
	addFitToDom(data.listId, data.entryId, data.fit, data.isQueue);
}

function entryAddedListener(event) {
	var data = JSON.parse(event.data);
	addNewEntry(data.listId, data.entry, data.groupId, data.isQueue);
}

function fitRemovedListener(event) {
	var data = JSON.parse(event.data);
	removeFitFromDom(data.listId, data.entryId, data.fitId)
}

function entryRemovedListener(event) {
	var data = JSON.parse(event.data);
	removeEntryFromDom(data.listId, data.entryId);
}

function gongListener(event) {
	if(gongEnabled()) {
		playGong();
	}
}

function noSSE() {
	var nosse = '<div class="alert alert-danger" role="alert"><p class="text-xs-center">We have had to disable <strong>features</strong> please consider upgrading your<a href="http://caniuse.com/#feat=eventsource"> browser</a>!</p></div>'
	document.getElementById("gong").innerHTML = nosse;
}

function getSSE() {
	var sse = new EventSource(getMetaData('api-sse')+"?events="+encodeURIComponent("waitlistUpdates,gong")+"&groupId="+encodeURIComponent(getMetaData("wl-group-id")));
	sse.addEventListener("fit-added", fitAddedListener);
	sse.addEventListener("fit-removed", fitRemovedListener);
	
	sse.addEventListener("entry-added", entryAddedListener);
	sse.addEventListener("entry-removed", entryRemovedListener);
	sse.addEventListener("invite-send", gongListener);
	
	sse.onerror = handleSSEError;
	return sse;
}

$(document).ready(
function() {
	eventSource = getSSE();
	if (refreshWl != undefined) {
		refreshWl();
	}
});

@@ -113,16 +113,35 @@
 efined;%0A
+var errorCount = 0;
 %0Afunctio
@@ -165,16 +165,17 @@
 event) %7B
+#
 %0A%09consol
@@ -225,16 +225,590 @@
 lose();%0A
+%09errorCount++;%0A%09if (errorCount %3C 2) %7B // our first error reconnect this instant%0A%09%09connectSSE();%0A%09%7D else if (errorCount %3E= 2 && errorCount %3C= 5) %7B  // 2-5 errors, try reconnect after 1s%0A%09%09setTimeout(connectSSE, 1000);%0A%09%7D else %7B // %3E 5 errors try reconnect after 10s%0A%09%09setTimeout(connectSSE, 10000);%0A%09%7D%0A%09event.target.close();%0A%7D%0A%0Afunction handleSSEOpen(event) %7B%0A%09console.log(%22SSE Open%22);%0A%09if (errorCount %3E 1) %7B%0A%09%09// refresh the page using json, to pull ALL the date, we might have missed sth%0A%09%09refreshWl();%0A%09%7D%0A%09errorCount = 0; // reset error counter%0A%7D%0A%0Afunction connectSSE() %7B%0A
 %09eventSo
@@ -1935,16 +1935,78 @@
 id%22)));%0A
+%09sse.onerror = handleSSEError;%0A%09sse.onopen = handleSSEOpen;%0A%09%0A
 %09sse.add
@@ -2290,37 +2290,8 @@
 %0A%09%0A%09
-sse.onerror = handleSSEError;
 %0A%09re
@@ -2336,32 +2336,22 @@
 on() %7B%0A%09
-eventSource = ge
+connec
 tSSE();%0A

make the SSE on error wait a longer delay if it keeps disconnecting, and reload the waitlist from full data IF it could not reconnect immediately