tweetcast/twisted/client/js/script.js
changeset 404 89968844eb7d
parent 403 dd1686ae5506
child 405 6626b728b142
equal deleted inserted replaced
403:dd1686ae5506 404:89968844eb7d
     1 // Web Socket definitions
       
     2 
       
     3 WEB_SOCKET_SWF_LOCATION = "lib/websocket-js/WebSocketMain.swf";
       
     4 WEB_SOCKET_DEBUG = true;
       
     5 var ws, tweets = [];
       
     6 
       
     7 $(document).ready(function() {
       
     8   ws = new WebSocket("ws://" + document.location.hostname + ":9000/");
       
     9   ws.onopen = function() {
       
    10   	console.log("open");
       
    11   }
       
    12   ws.onmessage = function(e) {
       
    13 	d = $.parseJSON(e.data);
       
    14 	console.log(d);
       
    15 	if (d.tweets) {
       
    16 		tweets = tweets.concat(d.tweets);
       
    17 		html = '';
       
    18 		for (var i = d.tweets.length - 1; i >= 0; i--) {
       
    19 			html += Mustache.to_html('<li class="tweet{{#annotations}} a_{{name}}{{/annotations}}" id="tweet_{{id}}"><img class="tweet_profile_image" src="{{profile_image_url}}" /><h4 class="tweet_title"><a href="http://twitter.com/{{screen_name}}" target="_blank">@{{screen_name}}:</a></h4><p class="tweet_created_at">{{created_at}}</p><p class="tweet_text">{{text}}</p></li>',d.tweets[i]);
       
    20 		}
       
    21 		console.log(html);
       
    22 		$("#tweetlist").prepend(html);
       
    23 	}
       
    24   };
       
    25   ws.onclose = function() {
       
    26 	console.log("close");
       
    27   };
       
    28   ws.onerror = function() {
       
    29 	console.log("error");
       
    30   };
       
    31   
       
    32   
       
    33 })