invisibleroads / socketIO-client

A socket.io client library for Python
http://pypi.python.org/pypi/socketIO-client
MIT License
447 stars 205 forks source link

Using namespace socket inside namespace class #183

Open skyguy126 opened 6 years ago

skyguy126 commented 6 years ago

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?

Hendrikto commented 6 years ago

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

skyguy126 commented 6 years ago

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