Created
May 15, 2012 21:07
The gist of creating a websocket connection
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
var request = new $.atmosphere.AtmosphereRequest(); | |
request.transport = 'websocket'; | |
request.url = "<c:url value='/twitter/concurrency'/>"; | |
request.contentType = "application/json"; | |
request.fallbackTransport = 'streaming'; | |
request.onMessage = function(response){ | |
buildTemplate(response); | |
}; | |
var subSocket = socket.subscribe(request); | |
function buildTemplate(response){ | |
if(response.state = "messageReceived"){ | |
var data = response.responseBody; | |
if (data) { | |
try { | |
var result = $.parseJSON(data); | |
$( "#template" ).tmpl( result ).hide().prependTo( "#twitterMessages").fadeIn(); | |
} catch (error) { | |
console.log("An error ocurred: " + error); | |
} | |
} else { | |
console.log("response.responseBody is null - ignoring."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment