Created
July 1, 2014 22:15
-
-
Save loganhasson/87b13dd255f953471320 to your computer and use it in GitHub Desktop.
boilerplate js for actioncontroller::live
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
function initialize() { | |
var source = new EventSource('/stream?name=Logan'); | |
source.addEventListener('message', function(e){ | |
console.log("Received "+e.data); | |
updateMessagesList(e.data); | |
}, false); | |
source.addEventListener('open', function(e){ | |
console.log('Connection was opened.'); | |
}, false); | |
source.addEventListener('error', function(e){ | |
if (e.readyState == EventSource.CLOSED) { | |
console.log("Connection was closed."); | |
} else { | |
console.log("Something else happened."); | |
} | |
}, false); | |
}; | |
function updateMessagesList(event){ | |
var message = $('<li>').text(event); | |
$('#messages').prepend(message); | |
}; | |
if ($('#messages').size() > 0) { | |
initialize(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment