擅长:python、mysql、java
<p>你可以给星际之门一次重击:<a href="http://boothead.github.com/stargate/" rel="nofollow">http://boothead.github.com/stargate/</a>和<a href="http://pypi.python.org/pypi/stargate/" rel="nofollow">http://pypi.python.org/pypi/stargate/</a>。</p>
<p>它建立在金字塔和eventlet之上(我也为eventlet提供了一点websocket支持和测试)。金字塔的最大优点是它有一个url映射到的资源的概念,而不仅仅是一个可调用的结果。因此,最终会得到一个映射到url结构的持久性资源图,而websocket连接只是路由并连接到这些资源。</p>
<p>所以你只需要做两件事:</p>
<pre><code>class YourView(WebSocketView):
def handler(self, websocket):
self.request.context.add_listener(websocket)
while True:
msg = websocket.wait()
# Do something with message
</code></pre>
<p>接收消息
以及</p>
<pre><code>resource.send(some_other_message)
</code></pre>
<p>这里resource是上面stargate.resource.WebSocketAwareContext的一个实例(与self.request.context一样),send方法将消息发送到与add_listener方法连接的所有客户端。</p>
<p>要向所有连接的客户端发布消息,只需调用<code>node.send(message)</code></p>
<p>我希望在接下来的一两周内能写一个小的示例应用程序来更好地演示这一点。</p>
<p>如果您需要帮助,可以在github上ping我。</p>