Hi,<div><br></div><div>I&#39;ve done work on the txWebSocket project in the past - it&#39;s now slightly out of date for thew new spec. You&#39;re using the fork by wulczer which supports the new protocol, so that&#39;s good. I have to update the main project at some point to incorporate his work.</div>
<div><br></div><div>Anyway, to answer your quesions...<br><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div><br></div><div>My problem is:</div>
<div>If I do this:</div><div><div>    site = WebSocketSite(root)</div><div>    site.addChatHandler(&#39;/room<b>1</b>&#39;, Chathandler)</div>

<div><div>    site.addChatHandler(&#39;/room<b>2</b>&#39;, Chathandler)</div></div><div>    site.addChatHandler(&#39;/room<b>3</b>&#39;, Chathandler)</div><div>    reactor.listenTCP(8080, site) </div></div><div><br></div>


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


<div><br></div></div></blockquote><div><br></div><div>Right, I see what your issue is. You really want a mapping from room resources to state (i.e. &#39;/room1&#39; -&gt; users1, etc). As you&#39;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&#39;ve been called at. I&#39;ll need to add this ability.</div>
<div><br></div><div>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.</div>
<div><br></div><div>I plan on making access to the site and uri be public which under the hood will access the above two attributes. </div><div><br></div><div>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.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div>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. </div>
<div>If you read this far ans have any (even it looks that stupid for you), might help right now (:</div></div></blockquote><div><br></div><div>If you&#39;re interested in a basic chatroom implementation, the idea you have sketched above is fine. However, if you&#39;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 <a href="http://redis.io/topics/pubsub">http://redis.io/topics/pubsub</a> and a Twisted library for Redis at <a href="https://github.com/rlotun/txRedis">https://github.com/rlotun/txRedis</a>. 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 &quot;fan out&quot; type logic as shown in your sendChat method).</div>
<div><br></div><div>I hope this helps - it isn&#39;t entirely satisfying I know, but the situation will improve soon.</div><div><br></div><div>Thanks,</div><div>Reza</div></div><br clear="all"><div><br></div>-- <br>Reza Lotun<br>
mobile: +44 (0)7521 310 763<br>email:  <a href="mailto:rlotun@gmail.com" target="_blank">rlotun@gmail.com</a><br>work:   <a href="mailto:rlotun@twitter.com" target="_blank">rlotun@twitter.com</a><br>@rlotun<br><br>
</div>