Block when define a Namespace
I used the following code, but the program blocked on socketio.define(...):
class TestNamespace(SocketIONamespace):
pass
socketio = SocketIO('127.0.0.1', 8081)
socketio.define(TestNamespace, '/test')
I traced the code and found that:
- There's a default namespace after create a instance of
SocketIOobject. - The
define()while blocked until all namespaces are connected. - In the
_should_stop_waiting()function, the connection is checked by namespace's_connectedproperty. - In the
_process_packet()function, the namespace's _connected property is set after received the connect message. - But, the default namespace's
_connectedhas never been set, so the define() is blocked.
And, I also checked the server's code:
- The connection build on
pollingfirst, and then upgrade towebsocket. - The sever send two packet by polling when client connected: the connect packet(type=0) and message (type=4) packet.
- But, on the client side, in
_get_engineIO_session()function, only connect packet is processed. and then upgrade towebsocket.
Finally, I found the block is caused by forgot set _connected to the default namespace.
The fix to suggest is appending self._on_connect(None, self.get_namespace()) on SocketIO.__init__ function.
By the way, don't forget change __version__ in your code. :-)
I also found that the define() function blocks forever--the examples in the documentation that use define() for custom namespaces don't work as intended. There is a function named _should_stop_waiting() in __init__.py that references the variable _connected, but the variable does not exist:
def _should_stop_waiting(self, for_connect=False, for_callbacks=False):
if for_connect:
for namespace in self._namespace_by_path.values():
is_namespace_connected = getattr(
namespace, '_connected', False)
If you remove the third parameter (False) from the getattr() call, an exception will occur because the BaseNamespace (SocketIONamespace) does not have an attribute named _connected, and getattr() is masking that fact. Is there possibly an update to namespaces.py that didn't get committed when the lines above were added to _should_stop_waiting()?
Possibly related to the problem: The code you get when you checkout version 0.6.5 has __version__ set to 0.6.3 in __init__.py.
Same issue here. I hope it'll be fixed soon.
As a temporary solution I use custom Namespace with hardcoded field _connected = True.
You need to include this namespace while initializing SocketIO and than also in socketIO.define(), because in _should_stop_waiting loop method returns False and stops iterating when default namespace '/' doesn't have _connected = True.
So here's my workaround:
class Namespace(LoggingNamespace):
_connected = True
def on_connect(self):
print('connected')
def on_error(self, data):
print('error')
with SocketIO('http://0.0.0.0', 8080, Namespace) as socketIO:
nsp = socketIO.define(Namespace, '/my_namespace')
nsp.emit('my message', 'hello')
socketIO.wait(seconds=1)
@ladislav-mizenko Great workaround. I just need this library for testing my webservice and it was really frustrating that it didn't work. Thank you very much for this!
Please include the fix in the actual version!
This is most likely happening because the namespace that you are trying to define is not defined on the server.
Duplicate of https://github.com/invisibleroads/socketIO-client/issues/84
Fixed in 2abcc91fb7f6c5a5d1da1f5edf2fda1d85890015
Please re-open this issue if it is still a problem.