Will subclass of WebSocketServerProtocol class creates new instance on every client connect?

#1

Hi,

I am facing an unexpected weird issue. It could be lack of my understanding of autobahn or I am missing something for sure.

Writing a server using Autobahn for simple web socket (not WAMP).

I have multiple classes in my server, the three main classes are:

  • ClientManager class # suppose to have client instance (one instance means one client) this class is sub class of WebSocketServerProtocol class. and all onConnect, onOpen and onMessage functions are there. on connect and disconnect clients, I am registering and unregistering client (self.factory.unregister(self)) the register and unregister methods are in SocketManager class. this class also have two properties self.id and self.groups (self.groups = []), where I want to keep per client id and joined groups name. Clients (JS) can join different groups, at joining groups I append group name in ClientManager class groups property (self.groups.append(groupName)).

  • SocketManager class # Suppose to have server instance, so If I create two web socket servers on two different port like (9000 and 9200) then there will be two instance of SocketManager class. This class is sub class of WebSocketServerFactory class. In this class I am keeping the methods to send message to all clients and all.

  • ServerManager class # this is not much doing but creating instance of SocketManager class to create servers, current only one at 9000.
    in class ServerManager there is nothing much than this method:

def initWsServer(self):

port = 9000

ws = WSManager(“ws://localhost:%d” % port, debug=True)

ws.protocol = ClientManager

reactor.listenTCP(9000, ws)

reactor.run()

The issue I am facing is: When I am adding (appending) an group name in group array for x client, some how it’s also adding in all other clients groups array. I want unique / different group arrays for each client.

Can any one tell me what is wrong I am doing in this. Incase I didn’t explain it correctly let me know what I am missing to make it more clear.

0 Likes

#2

Chuck,

I'm afraid, without looking at your actual code, it'll be hard to debug;)

That being said, if you want to maintain a list of groups joined per client, you could maintain a dictionary with keys being the client instance, and value being a list of groups:

class MyFactory(WebSocketServerFactory):

   def __init__(...):
      WebSocketServerFactory.__init__(...)
      self.groups = {}

   def join(self, proto, group):
      if not proto in self.groups:
         self.groups[proto] = set()
      if not group in self.groups[proto]:
         self.groups[proto].add(group)

   def leave(self, proto, group):
      if proto in self.groups and group in self.groups[proto]:
         self.groups[proto].remove(group)
         if len(self.groups[proto]) == 0:
            del self.groups[proto]

and call join() and leave() from your WebSocketServerProtocol.

/Tobias

WebSocketServerFactory

···

Am 19.01.2014 19:19, schrieb Chuck James:

Hi,

        I am facing an unexpected weird issue. It could be lack of my
understanding of autobahn or I am missing something for sure.

Writing a server using Autobahn for simple web socket (not WAMP).
I have multiple classes in my server, the three main classes are:

  * ClientManager class # suppose to have client instance (one instance
    means one client) this class is sub class of WebSocketServerProtocol
    class. and all onConnect, onOpen and onMessage functions are there.
    on connect and disconnect clients, I am registering and
    unregistering client (self.factory.unregister(self)) the register
    and unregister methods are in *SocketManager* class. this class also
    have two properties self.id and self.groups (self.groups = []),
    where I want to keep per client id and joined groups name. Clients
    (JS) can join different groups, at joining groups I append group
    name in ClientManager class groups property
    (self.groups.append(groupName)).

  * *SocketManager* class # Suppose to have server instance, so If I
    create two web socket servers on two different port like (9000 and
    9200) then there will be two instance of SocketManager class. This
    class is sub class of WebSocketServerFactory class. In this class I
    am keeping the methods to send message to all clients and all.

  * ServerManager class # this is not much doing but creating instance
    of SocketManager class to create servers, current only one at 9000.

in class ServerManager there is nothing much than this method:
     def initWsServer(self):
         port = 9000
         ws = WSManager("ws://localhost:%d" % port, debug=True)
         ws.protocol = ClientManager
         reactor.listenTCP(9000, ws)
         reactor.run()

The issue I am facing is: When I am adding (appending) an group name in
group array for x client, some how it's also adding in all other clients
groups array. I want unique / different group arrays for each client.

Can any one tell me what is wrong I am doing in this. Incase I didn't
explain it correctly let me know what I am missing to make it more clear.

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes