Created
July 17, 2014 07:46
-
-
Save mmcc/c013809fac73396006b6 to your computer and use it in GitHub Desktop.
EventSource example
This file contains 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 initEventSource(route) { | |
var source = new EventSource(route); | |
source.onopen = connectionEstablished; | |
source.onmessage = messageReceived; | |
source.onerror = connectionError; | |
return source; | |
} | |
function connectionEstablished(event) { | |
console.log('Connection established'); | |
} | |
function messageReceived(event) { | |
var msg = JSON.parse(event.data); | |
} | |
function connectionError(event) { | |
if (e.readyState === EventSource.CLOSED) { | |
console.log('Connection closed.'); | |
} else { | |
console.log('EventSource error', event); | |
} | |
} | |
$(function() { | |
var testSource = initEventSource('/test'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment