Activate your free membership today | Log-in

Thursday, December 27th, 2007

Django and Comet

Category: Articles, Comet

Arena Albionu has written about Django and Comet using the Orbited Python event driven comet server.

The article walks through the hello world of Comet... a chat server. The JavaScript looks like this:

JAVASCRIPT:
  1.  
  2. function processGetPost()
  3.         {
  4.         var myajax=ajaxpack.ajaxobj
  5.         var myfiletype=ajaxpack.filetype
  6.         if (myajax.readyState == 4)
  7.                 { //if request of file completed
  8.                 if (myajax.status==200 || window.location.href.indexOf("http")==-1)
  9.                         { //if request was successful or running script locally
  10.                         if (myfiletype=="txt")
  11.                         alert(myajax.responseText)
  12.                         else
  13.                         alert(myajax.responseXML)
  14.                         }
  15.                 }
  16.         }
  17.  
  18.  
  19. function connect()
  20. {
  21.   var nick = document.getElementById('nickname').value;
  22.   Orbited.connect(chat_event, nick, "/chat", "0");
  23.   ajaxpack.getAjaxRequest("/join/" + nick + "/", "", processGetPost, "txt");
  24. }
  25.  
  26.  
  27. chat_event = function(data) {
  28.   var chat_box = document.getElementById('box');
  29.   var div = window.parent.document.createElement('div');
  30.   div.className = "event";
  31.   div.innerHTML = data;
  32.   chat_box.appendChild(div);
  33.   chat_box.scrollTop = chat_box.scrollHeight;
  34. }
  35.  
  36. function send_msg() {
  37.   var msg = document.getElementById('chat').value;
  38.   var nick = document.getElementById('nickname').value;
  39.   ajaxpack.getAjaxRequest("/send/" + nick + "/" + msg + "/", "", processGetPost, "txt");
  40. }
  41.  

Posted by Dion Almaer at 12:08 am

+++--
3.3 rating from 13 votes

2 Comments »

Comments feed TrackBack URI

Dion, Could you replace the current Orbited link with a link to www.orbited.org

Thanks!

Comment by heyadayo — December 27, 2007

It’s interesting to see how the Comet stuff starts to shape up. Back in 2006, I wrote a simple Comet server for Server-Sent DOM Events using Python and Twisted.

Comment by Jani — December 27, 2007

Leave a comment

You must be logged in to post a comment.