[Twisted-web] Coding websockets server.

João Ricardo Mattos e Silva joaoricardo000 at gmail.com
Mon Sep 5 23:49:57 EDT 2011


Hey,
my first e-mail on this list, I`m beginning to code with twisted, started
after a research about building c10k servers.. (with python)
I am using this lib, heard it is the best for working with websockets
(correct me if you know another)
https://github.com/wulczer/txWebSocket

Well,
There is what I want to do:
I have:

    site = WebSocketSite(root)
    site.addChatHandler('/room*1*', Chathandler)
    reactor.listenTCP(8080, site)

where

class Chathandler(WebSocketHandler):
    users = set()
    def __init__(self, transport):
        WebSocketHandler.__init__(self, transport)

    def __del__(self):
        print 'Deleting handler'

    def setUsers(self,usr):
        self.users = usr

    def frameReceived(self, frame):
        adr = self.transport.getPeer()
        print "Msg rcv from: ", adr
        self.sendChat(adr,frame)

    def connectionMade(self):
        print 'Connected to client.'
        self.users.add(self)

    def connectionLost(self, reason):
        print 'Lost connection.'
        if self in self.users:
            self.users.remove(self)

    def sendChat(self,fr,msg):
        for u in self.users:
            u.transport.write(str(fr)+msg)

My problem is:
If I do this:
    site = WebSocketSite(root)
    site.addChatHandler('/room*1*', Chathandler)
    site.addChatHandler('/room*2*', Chathandler)
    site.addChatHandler('/room*3*', Chathandler)
    reactor.listenTCP(8080, site)

will not work (every room will comunicate with every room), because
the users = set() will be globally between the handlers..
If I put on the __init__, every call on /roomX will have your personal set()
of users including only themselfs.

I am kind stuck on this, I`m new to websocket and server programming, so I
don`t even know if is the best way to code a chatroom server.
If you read this far ans have any (even it looks that stupid for you), might
help right now (:

Thanks,

-- 
*João Ricardo Mattos e Silva*

Graduando em Ciência da Computação na Universidade Federal de Santa Catarina

*Cel: *+55 (48) 96190063  |  *Skype:* jricardomsilva  | * Msn: *
joaoricardo at globalite.com.br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20110906/212247a7/attachment-0001.htm 


More information about the Twisted-web mailing list