0

I might be on the wrong path but I'm trying to do something like the following however the token never gets returned from on_message

import socketio

sio = socketio.Client()
token = ""

def on_message(data):
    token = data.get('token')
    token = str(token)
    return token

sio.connect('http://192.168.155.249')

sio.emit("postLogin",{"email":"someone@somewhere.com","password":"password"})
token = sio.on('user', on_message)

sio.emit("saveDocument",{"title":"","content":"asdfas","id":"2","token":token})

While i could put my saveDocument emit inside on_message i have other places i need to use token (will be using Requests library to post the data elsewhere later). Beginner coder and i see mentions of callbacks in the python-socketio documentation however i'm not sure if they can help with what i'm trying to accomplish.

  • How does the server return the token? – Miguel Grinberg Oct 18 '20 at 21:48
  • According to the socketio source code it only returns the token to the callback (in this case on_messsage in 'data').I would expect if i use the return in on_message it should then return the token to the global token variable in main. – Scalapani Oct 20 '20 at 00:23
  • to be clear i have confirmed that the token is valid and available within on_message so the issue is not related to the server response. The issue is on my side related to the socketio.Client().emit function not passing the token to main – Scalapani Oct 20 '20 at 00:31
  • You are overthinking this. Is this your own server? Maybe you should show the code that handles the `postLogin` event and returns the token so that I understand what you need to do on the client side. – Miguel Grinberg Oct 21 '20 at 16:14

0 Answers0