Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

Python socketio badnamespaceerror exception on one device but not working on another

Hi all I have the following code but for some reason I keep getting the following error but it seems to work on a colleagues pc. We can't seem to figure out why this won't work on mine.

We have also double checked that we're importing the same socketio using dir()

I've tried specifying the namespace both on sio.connect and in the sio.emit but still no luck!

socketio.exceptions.BadNamespaceError: / is not a connected namespace.

bearerToken = 'REDACT'

core = 'REDACT'
output = 'REDACT'


import socketio
import json

def getListeners(token, coreUrl, outputId):
    sio = socketio.Client(reconnection_attempts=5, request_timeout=5)
    sio.connect(url=coreUrl, transports='websocket')

    @sio.on('mwedge:batch:stats')
    def batchStats(data):
        if (outputId in data['outputStats']):
            listeners = data['outputStats'][outputId][16]
            print("Number of listeners ", len(listeners))
            ips = []
            for listener in listeners:
                ips.append(listener[1])
            print("Ips", ips)

    def authCallback(data):
        print(json.dumps(data))

    sio.emit(event='auth',
    data={
        'token': token
    },
    callback=authCallback)


getListeners(bearerToken, core, output)
question from:https://stackoverflow.com/questions/65851664/python-socketio-badnamespaceerror-exception-on-one-device-but-not-working-on-ano

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Socket.IO connection involves a number of exchanges between the client and the server. The connect() function initiates this process, but this continues in the background. The connection ends when the handler for your connect event is invoked. At this point you can emit.

The problem with your code is that you are not waiting until the connection handshakes are completed, so your emit() call happens before there is a connection established. The solution is to add a connect event handler, and move your emit() call there.

As an additional note, I suggest you set up your event handlers before you call the connect() function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...