Emit a custom event to the server and wait for the response. This method issues an emit with a callback and waits for the callback to be invoked before returning. If the callback isn't invoked before the timeout, then a ``TimeoutError`` exception is raised. If the So
(self, event, data=None, namespace=None, timeout=60)
| 260 | callback=callback) |
| 261 | |
| 262 | def call(self, event, data=None, namespace=None, timeout=60): |
| 263 | """Emit a custom event to the server and wait for the response. |
| 264 | |
| 265 | This method issues an emit with a callback and waits for the callback |
| 266 | to be invoked before returning. If the callback isn't invoked before |
| 267 | the timeout, then a ``TimeoutError`` exception is raised. If the |
| 268 | Socket.IO connection drops during the wait, this method still waits |
| 269 | until the specified timeout. |
| 270 | |
| 271 | :param event: The event name. It can be any string. The event names |
| 272 | ``'connect'``, ``'message'`` and ``'disconnect'`` are |
| 273 | reserved and should not be used. |
| 274 | :param data: The data to send to the server. Data can be of |
| 275 | type ``str``, ``bytes``, ``list`` or ``dict``. To send |
| 276 | multiple arguments, use a tuple where each element is of |
| 277 | one of the types indicated above. |
| 278 | :param namespace: The Socket.IO namespace for the event. If this |
| 279 | argument is omitted the event is emitted to the |
| 280 | default namespace. |
| 281 | :param timeout: The waiting timeout. If the timeout is reached before |
| 282 | the server acknowledges the event, then a |
| 283 | ``TimeoutError`` exception is raised. |
| 284 | |
| 285 | Note: this method is not thread safe. If multiple threads are emitting |
| 286 | at the same time on the same client connection, messages composed of |
| 287 | multiple packets may end up being sent in an incorrect sequence. Use |
| 288 | standard concurrency solutions (such as a Lock object) to prevent this |
| 289 | situation. |
| 290 | """ |
| 291 | callback_event = self.eio.create_event() |
| 292 | callback_args = [] |
| 293 | |
| 294 | def event_callback(*args): |
| 295 | callback_args.append(args) |
| 296 | callback_event.set() |
| 297 | |
| 298 | self.emit(event, data=data, namespace=namespace, |
| 299 | callback=event_callback) |
| 300 | if not callback_event.wait(timeout=timeout): |
| 301 | raise exceptions.TimeoutError() |
| 302 | return callback_args[0] if len(callback_args[0]) > 1 \ |
| 303 | else callback_args[0][0] if len(callback_args[0]) == 1 \ |
| 304 | else None |
| 305 | |
| 306 | def disconnect(self): |
| 307 | """Disconnect from the server.""" |