twisted:如何删除静态资源?

时间:2015-06-02 07:25:54

标签: python-2.7 autobahn twisted.web twisted.internet autobahnws

我有一个基本的TCP服务器,用于连接客户端。客户端连接并发送启动websocket资源所需的数据。使用TCP客户端发送的这些细节,我想在扭曲的Web资源下添加一个autobahn websocket资源作为孩子。 当客户端断开连接时,我想从扭曲的网络资源中删除这个孩子。请建议,实施此操作的最佳方法是什么?我可以使用resource.delEntity(child)吗?

到目前为止,代码如下所示:

from twisted.internet.protocol import Protocol, Factory
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol
from autobahn.twisted.resource import WebSocketResource

class ProtoPS(WebSocketServerProtocol):
  def onMessage(self,payload,isBinary):
    if not isBinary:
        print("Text message received: {}".format(payload.decode('utf8')))
    self.sendMessage(payload,isBinary)

class BaseResource:
  def __init__(self,proto,vid):
    self.vid = vid
    self.factory = WebSocketServerFactory()
    self.factory.protocol = proto
    self.resource = WebSocketResource(self.factory)
    wsroot.putChild(self.vid,self.resource)

class BackendProto(Protocol):
  def __init__(self):
    self.SERVICEMAP = {}

  def dataReceived(self,data):
    msg = json.loads(data)
    if ('cmd' in msg) and (msg['cmd'] == "create"):
         self.vid = msg['client']['id']
         self.SERVICEMAP[self.vid] = BaseResource(ProtoPS,self.vid)

  def connectionLost(self,reason):
    wsroot.delEntity(self.vid)
    del self.SERVICEMAP[self.vid]

class BackendFactory(Factory):
  protocol = BackendProto

if __name__ == '__main__':
  reactor.listenTCP(8081,BackendFactory())
  wsroot = Data("","text/plain")
  wssite = Site(wsroot)
  reactor.listenTCP(9000,wssite)

0 个答案:

没有答案