Thursday, December 27th, 2007

Django and Comet

Category: Articles, Comet

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

Posted by Dion Almaer at 12:08 am
3 Comments

+++--
3.2 rating from 17 votes

3 Comments »

Comments feed TrackBack URI

Dion, Could you replace the current Orbited link with a link to http://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

I just wrote up a basic intro to using Django with Orbited:

http://darkporter.com/?p=7

Comment by jpsimons — September 9, 2008

Leave a comment

You must be logged in to post a comment.