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)
| 272 | callback=callback) |
| 273 | |
| 274 | async def call(self, event, data=None, namespace=None, timeout=60): |
| 275 | """Emit a custom event to the server and wait for the response. |
| 276 | |
| 277 | This method issues an emit with a callback and waits for the callback |
| 278 | to be invoked before returning. If the callback isn't invoked before |
| 279 | the timeout, then a ``TimeoutError`` exception is raised. If the |
| 280 | Socket.IO connection drops during the wait, this method still waits |
| 281 | until the specified timeout. |
| 282 | |
| 283 | :param event: The event name. It can be any string. The event names |
| 284 | ``'connect'``, ``'message'`` and ``'disconnect'`` are |
| 285 | reserved and should not be used. |
| 286 | :param data: The data to send to the server. Data can be of |
| 287 | type ``str``, ``bytes``, ``list`` or ``dict``. To send |
| 288 | multiple arguments, use a tuple where each element is of |
| 289 | one of the types indicated above. |
| 290 | :param namespace: The Socket.IO namespace for the event. If this |
| 291 | argument is omitted the event is emitted to the |
| 292 | default namespace. |
| 293 | :param timeout: The waiting timeout. If the timeout is reached before |
| 294 | the server acknowledges the event, then a |
| 295 | ``TimeoutError`` exception is raised. |
| 296 | |
| 297 | Note: this method is not designed to be used concurrently. If multiple |
| 298 | tasks are emitting at the same time on the same client connection, then |
| 299 | messages composed of multiple packets may end up being sent in an |
| 300 | incorrect sequence. Use standard concurrency solutions (such as a Lock |
| 301 | object) to prevent this situation. |
| 302 | |
| 303 | Note 2: this method is a coroutine. |
| 304 | """ |
| 305 | callback_event = self.eio.create_event() |
| 306 | callback_args = [] |
| 307 | |
| 308 | def event_callback(*args): |
| 309 | callback_args.append(args) |
| 310 | callback_event.set() |
| 311 | |
| 312 | await self.emit(event, data=data, namespace=namespace, |
| 313 | callback=event_callback) |
| 314 | try: |
| 315 | await asyncio.wait_for(callback_event.wait(), timeout) |
| 316 | except asyncio.TimeoutError: |
| 317 | raise exceptions.TimeoutError() from None |
| 318 | return callback_args[0] if len(callback_args[0]) > 1 \ |
| 319 | else callback_args[0][0] if len(callback_args[0]) == 1 \ |
| 320 | else None |
| 321 | |
| 322 | async def disconnect(self): |
| 323 | """Disconnect from the server. |