Coding websockets server.

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@globalite.com.br

Hey, I'm also using txwebsocket for this project. See https://github.com/phrearch/hwios kind regards, Jeroen 2011/9/6 João Ricardo Mattos e Silva <joaoricardo000@gmail.com>
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@globalite.com.br
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

Hi, I've done work on the txWebSocket project in the past - it's now slightly out of date for thew new spec. You're using the fork by wulczer which supports the new protocol, so that's good. I have to update the main project at some point to incorporate his work. Anyway, to answer your quesions...
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.
Right, I see what your issue is. You really want a mapping from room resources to state (i.e. '/room1' -> users1, etc). As you've noticed the users set is a class-level construct, while the __init__ for the handler is called on every connection. What you really want to be able to do is associate this resource mapping on the site object. Unfortunately there is currently no direct way to gain access to this object, and there is no easy way to determine the resource you've been called at. I'll need to add this ability. For the time being I suggest to subclass WebSocketSite, and then access the object via private access (I know this is *bad*, but there will be a better option soon) in the handler as self.transport._request.site and the uri as self.transport._request.uri. That way you can access a mapping on the site object. I plan on making access to the site and uri be public which under the hood will access the above two attributes. Sorry about the state of txWebsocket - development on it paused as the websocket spec was going through a state of flux. It seems to have settled down recently, so now is the time for me to really sort out these issues. Thanks for bringing it to my attention.
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 (:
If you're interested in a basic chatroom implementation, the idea you have sketched above is fine. However, if you're looking to add a tad more robustness, might I suggest you implement the fan out logic in Redis using its pub-sub functionality? Check out http://redis.io/topics/pubsub and a Twisted library for Redis at https://github.com/rlotun/txRedis. Websockets and your txWebsocket code can then only need to act as a transport to the end-user (that is, there is mapping from your getPeer() calls on your self.transport to users, and every internal subscription per user is then fed back as websocket data to the browser. That way you can avoid "fan out" type logic as shown in your sendChat method). I hope this helps - it isn't entirely satisfying I know, but the situation will improve soon. Thanks, Reza -- Reza Lotun mobile: +44 (0)7521 310 763 email: rlotun@gmail.com work: rlotun@twitter.com @rlotun
participants (3)
-
Jeroen van Veen
-
João Ricardo Mattos e Silva
-
Reza Lotun