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

I have no idea why this simple code isn't working. #204

Open ilovetogetspamed opened 5 years ago

ilovetogetspamed commented 5 years ago

I'm having trouble with my on_...._response handlers. They don't seem to get an of the data being sent by the server.

I'm sure I'm doing something wrong and was hoping you could help.

The code is here: https://gist.github.com/ilovetogetspamed/b4e013fd860a8d3cf753205747bb9cb6

I tried it two ways with similar results.

Thanks

ilovetogetspamed commented 5 years ago

I was able to get to the data by adding an 'on_event' handler to my client.

def on_event(self, event, *args):
        super(RFIDNamespace, self).on_event(event, *args)
        print ('CLIENT: on_event', event, args)

This is a kludge, of course, because what's the point of having defined callbacks if they don't fire with data being passed to them (problem is with the client library, not Flask-SocketIO). I was pretty much following your example code verbatim and it didn't work as expected.

At least I can move forward by testing the 'event' variable to see if it was 'on_tag_gained' or 'on_tag_lost'.

lohithNCB commented 5 years ago

@ilovetogetspamed two ways can be followed to call defined callbacks

Case 1: if we are defining the custom_call_back event name other_than default call_back names. then there is a need for explicitly defining the call_back_names like below.

class RFIDNamespace(BaseNamespace):

    def on_tag_gained_response(self, *args):
        print("tag-----------gain---------response------------")
        print('CLIENT: on_tag_gained_response', args)

    def on_tag_lost_response(self, *args):
        print("tag----------------lost-----------response------")
        print('CLIENT: on_tag_lost_response', args)

    def on_disconnect(self):
        print('CLIENT: disconnected')

    def on_connect(self):
        print('CLIENT: connected')

    # def on_event(self, event, *args):
    #     super(RFIDNamespace, self).on_event(event, *args)
    #     print('CLIENT: on_event here i am', event, args)

socketIO = SocketIO('127.0.0.1', 10001)
rfid_namespace = socketIO.define(RFIDNamespace, '/rfid')

rfid_namespace.emit('tag_gained', {'tag': 'operator'})
rfid_namespace.on('tag_lost', rfid_namespace.on_tag_lost_response)
rfid_namespace.emit('tag_lost', {'tag': 'operator'})
rfid_namespace.on('tag_gained', rfid_namespace.on_tag_gained_response)
socketIO.wait(seconds=1)
rfid_namespace.disconnect()

case 2: which is exactly what you were expecting. i.e triggering the defined call-backs on its own.

just rename the method-names

#from
def on_tag_gained_response(self, *args):
        print("tag-----------gain---------response------------")
        print('CLIENT: on_tag_gained_response', args)
   #to
    def on_tag_gained(self, *args):
        print("tag-----------gain---------response------------")
        print('CLIENT: on_tag_gained_response', args)

same for "on_tag_lost_response" to "on_tag_lost"
doing so everything should work as expected... :)

ilovetogetspamed commented 5 years ago

OK , Thanks, I’ll try that.

On Dec 11, 2018, at 4:08 AM, lohithNCB notifications@github.com wrote:

@ilovetogetspamed https://github.com/ilovetogetspamed two ways can be followed to call defined callbacks

Case 1: if we are defining the custom_call_back event name other_than default call_back names. then there is a need for explicitly defining the call_back_names like below.

`class RFIDNamespace(BaseNamespace):

def on_tag_gained_response(self, *args): print("tag-----------gain---------response------------") print('CLIENT: on_tag_gained_response', args)

def on_tag_lost_response(self, *args): print("tag----------------lost-----------response------") print('CLIENT: on_tag_lost_response', args)

def on_disconnect(self): print('CLIENT: disconnected')

def on_connect(self): print('CLIENT: connected')

def on_event(self, event, *args):

super(RFIDNamespace, self).on_event(event, *args)

print('CLIENT: on_event here i am', event, args)

socketIO = SocketIO('127.0.0.1', 10001) rfid_namespace = socketIO.define(RFIDNamespace, '/rfid')

rfid_namespace.emit('tag_gained', {'tag': 'operator'}) rfid_namespace.on('tag_lost', rfid_namespace.on_tag_lost_response) rfid_namespace.emit('tag_lost', {'tag': 'operator'}) rfid_namespace.on('tag_gained', rfid_namespace.on_tag_gained_response) socketIO.wait(seconds=1) rfid_namespace.disconnect() `

case 2: which is exactly what you were expecting. i.e triggering the defined call-backs on its own.

just rename the method-names

from

def on_tag_gained_response(self, *args): print("tag-----------gain---------response------------") print('CLIENT: on_tag_gained_response', args)

to

def on_tag_gained(self, *args):
    print("tag-----------gain---------response------------")
    print('CLIENT: on_tag_gained_response', args)

same for "on_tag_lost_response" to "on_tag_lost" doing so everything should work as expected... :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/invisibleroads/socketIO-client/issues/204#issuecomment-446125813, or mute the thread https://github.com/notifications/unsubscribe-auth/ADHZnCu1JILv05TR3EC2SyphbqGUIAeCks5u33YLgaJpZM4Y8dCy.