Here are my quick notes on trying out the websocket example in Twisted's websocket branch. The documentation is here. The Twisted ticket is here. This came about after some conversation with @clemesha on Twitter.
(A Web Socket is a new, still-in-development technology, introduced in HTML5, and may be used for real-time web applications. It provides a simple (maybe better) alternative to existing Comet technology.)
(I am no expert on Web Sockets. I just think they are cool and want to start using them.)
$ cd ~/lib/python-environments $ virtualenv --no-site-packages --distribute twisted-websocket-branch $ pip install -E twisted-websocket-branch/ -e svn+svn://svn.twistedmatrix.com/svn/Twisted/branches/websocket-4173-2
import sys from twisted.python import log from twisted.internet import reactor from twisted.web.static import File from twisted.web.websocket import WebSocketHandler, WebSocketSite class Echohandler(WebSocketHandler): def frameReceived(self, frame): log.msg("Received frame '%s'" % frame) self.transport.write(frame + "\n") def main(): log.startLogging(sys.stdout) root = File(".") site = WebSocketSite(root) site.addHandler("/ws/echo", Echohandler) reactor.listenTCP(8080, site) reactor.run() if __name__ == "__main__": main()
$ source ~/lib/python-environments/twisted-websocket-branch/bin/activate
$ cd ~/wsdemo $ python demo.py
2010-05-25 21:47:46-0700 [-] Log opened. 2010-05-25 21:47:46-0700 [-] twisted.web.websocket.WebSocketSite starting on 8080 2010-05-25 21:47:46-0700 [-] Starting factory <twisted.web.websocket.WebSocketSite instance at 0x94243ac> 2010-05-25 21:47:56-0700 [HTTPChannel,0,127.0.0.1] 127.0.0.1 - - [26/May/2010:04:47:56 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2" 2010-05-25 21:47:56-0700 [HTTPChannel,1,127.0.0.1] 127.0.0.1 - - [26/May/2010:04:47:56 +0000] "GET /favicon.ico HTTP/1.1" 404 145 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2" 2010-05-25 21:48:16-0700 [HTTPChannel,2,127.0.0.1] Received frame 'hello' 2010-05-25 21:48:25-0700 [HTTPChannel,2,127.0.0.1] Received frame 'Twisted+Websocket!'
websockets are awesome! I wanted to expand this to send messages to all transports ( connected clients ). I simply made a transport array ( appending on connectionMade and removing on connectionLost ), and sendt to all available transports for every sendMessage.
It works, but I'm wondering if its a good way to go about doing that? I haven't used twisted a lot before, so there might be something I missed ;)
Hi Asbjørn, I'd like to do this also-- care to share a code snippet? (Sorry I don't know if there is a better way.)
Thats with rushman's tx-websockets as base ( http://bitbucket.org/rushman/tx-websockets/src ). Thats not exactly equivalent to what you've got, but hopefully you'll get the idea :P I guess you have to extend init and connectionLost in addition to updating your frameReceived...? Something like that.
Let me know if you get it working and/or make some new discoveries :)
Asbjørn, Thanks for the code! Ok, so you started with tx-websockets... I had initially thought this Twisted branch was the best option, but I may need to relook at alternatives. Thanks again. I'll add updates if I make any progress. Let me know also if you have any new info.
Hi, please how can I checkout the websocket branch?
svn co svn://svn.twistedmatrix.com/svn/Twisted/branches/websocket-4173-2
does not work for me.
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »