Python-socketio: Class-Based Namespaces

Created on 14 Jun 2017  路  4Comments  路  Source: miguelgrinberg/python-socketio

Hi,

I'm just trying to use a class-based namespace in your (wsgi) latency example, using eventlet as the async_mode, but the pings do not appear to be transmitting.

In latency.py, I have simply added the class and custom namespace:

# @sio.on('ping_from_client')
# def ping(sid):
#     sio.emit('pong_from_server', room=sid)

class StreamNamespace(socketio.Namespace):
    def on_ping_from_client(self, sid):
        sio.emit('pong_from_server', room=sid)

sio.register_namespace(StreamNamespace('/stream'))

And in tempates/latency.html, added the same custom namespace:

{# var socket = io.connect('http://' + document.domain + ':' + location.port); #}
var socket = io.connect('http://' + document.domain + ':' + location.port + '/stream');

Am I missing something?

Thanks for any help.

question

All 4 comments

Not sure if this is the only problem, but sio.emit() needs to be given the /stream namespace.

Yes, that was it. Thanks!

        sio.emit('pong_from_server', room=sid, namespace='/stream')

I had assumed the sio server would get that from the namespace handler registered.

You can use self.emit() and then the namespace will be obtained from the namespace object.

Ok sure, thanks again!

Was this page helpful?
0 / 5 - 0 ratings