how to delete static resource

#1

I have a basic TCP server implemented in twisted, to which a client is connected. The client connects and sends data necessary to start a websocket resource. Using these details sent by the TCP client, I want to add an autobahn websocket resource as a child under a twisted web resource. and when the client disconnects, I want to remove this child from the twisted web resource. please suggest, what should be the best way to implement this? Can I use resource.delEntity(child)? How? whats the best way of stopping a factory?

so far, the code looks like this:

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)

Enter code here…

``

0 Likes

#2

Dear All,

is it possible to have the websocket resource as a dynamic resource? In this example, it is nicely explained how to make websocket as a static resource.

I tried doing the following. its not working:

class PSProto(WebSocketServerProtocol):

def onConnect(self,request):

	print(request.peer)

def onOpen(self):

	self.factory.register(self)

def onMessage(self,payload,isBinary):

	if isBinary:

		print("Length: {0}".format(len(payload)))

	else:

		print("Message: {0}".format(payload.decode('utf-8')))

def connectionLost(self,reason):

	WebSocketServerProtocol.connectionLost(self,reason)

	self.factory.unregister(self)

class PSFactory(WebSocketServerFactory):

#def __init__(self, wsuri, debug=False):

	#self.clients = []

	#WebSocketServerFactory.__init__(self, wsuri, debug=debug, debugCodePaths=debug)

def __init__(self):

	self.clients = []

	WebSocketServerFactory.__init__(self)

def register(self,client):

	if client not in self.clients:

		self.clients.append(client)

		print("client {0} registered".format(client.peer))

def unregister(self,client):

	if client in self.clients:

		self.clients.remove(client)

		print("client {0} unregistered".format(client.peer))

class CamFeed(WebSocketResource):

def __init__(self,name):

	self.name = name

	self.factory = PSFactory()

	self.factory.protocol = PSProto

	self.factory.startFactory()

	WebSocketResource.__init__(self,self.factory)

def render(self,request):

	return WebSocketResource.render(self,request)

class View(Resource):

def getChild(self,name,request):

	return CamFeed(name)

``

Do I have to put it as a static resource always?

···

On Monday, June 8, 2015 at 3:23:51 PM UTC+5:30, Chetan Naik wrote:

I have a basic TCP server implemented in twisted, to which a client is connected. The client connects and sends data necessary to start a websocket resource. Using these details sent by the TCP client, I want to add an autobahn websocket resource as a child under a twisted web resource. and when the client disconnects, I want to remove this child from the twisted web resource. please suggest, what should be the best way to implement this? Can I use resource.delEntity(child)? How? whats the best way of stopping a factory?

so far, the code looks like this:

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)

Enter code here…

``

0 Likes

#3

That seems to work for me if I set View as the root of a Site. Can you post a complete example or describe what’s not working?

···

On Sunday, 23 August 2015 01:11:43 UTC-6, Chetan Naik wrote:

I tried doing the following. its not working:


meejah

0 Likes