socketIO-client icon indicating copy to clipboard operation
socketIO-client copied to clipboard

Using namespace socket inside namespace class

Open skyguy126 opened this issue 6 years ago • 2 comments

I am creating a socket connection as follows:


class ClientNamespace(BaseNamespace):

    def on_connect(self):
        logger.info("connected")
        clientSocket.emit("descriptor", "some cool data")

    def on_disconnect(self):
        logger.info("disconnected")

    def on_reconnect(self):
        logger.info("reconnected")

socketIO = SocketIO('127.0.0.1/sock', 80, Namespace=ClientNamespace)
clientSocket = socketIO.define(ClientNamespace, '/clients')
socketIO.wait() # listen forever

The issue is that the script immediately crashes as it says clientSocket is not defined when on_connect is called. I believe this is due to the fact that the define method instiantates the class before setting clientSocket to the namespace socket.

If I use socketIO.emit(. . .) inside ClientNamespace the server (nodejs socket.io 1.x) never receives any events in /clients which is what I need it to do. Am I doing something wrong or are there any workarounds?

skyguy126 avatar May 03 '18 17:05 skyguy126

I think you can just use self.emit. Does that solve your problem?

Hendrikto avatar May 16 '18 23:05 Hendrikto

@Hendrikto I will try again maybe I did something else before....

skyguy126 avatar May 17 '18 07:05 skyguy126