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)
site = WebSocketSite(root)
site.addChatHandler('/room1', 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('/room1', Chathandler)
site.addChatHandler('/room2', Chathandler)
site.addChatHandler('/room3', 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 SilvaGraduando em Ciência da Computação na Universidade Federal de Santa Catarina
Cel: +55 (48) 96190063 | Skype: jricardomsilva | Msn: joaoricardo@globalite.com.br