Emit a custom event to a client 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 Sock
(self, event, data=None, to=None, sid=None, namespace=None,
timeout=60, ignore_queue=False)
| 225 | callback=callback, ignore_queue=ignore_queue) |
| 226 | |
| 227 | async def call(self, event, data=None, to=None, sid=None, namespace=None, |
| 228 | timeout=60, ignore_queue=False): |
| 229 | """Emit a custom event to a client and wait for the response. |
| 230 | |
| 231 | This method issues an emit with a callback and waits for the callback |
| 232 | to be invoked before returning. If the callback isn't invoked before |
| 233 | the timeout, then a ``TimeoutError`` exception is raised. If the |
| 234 | Socket.IO connection drops during the wait, this method still waits |
| 235 | until the specified timeout. |
| 236 | |
| 237 | :param event: The event name. It can be any string. The event names |
| 238 | ``'connect'``, ``'message'`` and ``'disconnect'`` are |
| 239 | reserved and should not be used. |
| 240 | :param data: The data to send to the client or clients. Data can be of |
| 241 | type ``str``, ``bytes``, ``list`` or ``dict``. To send |
| 242 | multiple arguments, use a tuple where each element is of |
| 243 | one of the types indicated above. |
| 244 | :param to: The session ID of the recipient client. |
| 245 | :param sid: Alias for the ``to`` parameter. |
| 246 | :param namespace: The Socket.IO namespace for the event. If this |
| 247 | argument is omitted the event is emitted to the |
| 248 | default namespace. |
| 249 | :param timeout: The waiting timeout. If the timeout is reached before |
| 250 | the client acknowledges the event, then a |
| 251 | ``TimeoutError`` exception is raised. |
| 252 | :param ignore_queue: Only used when a message queue is configured. If |
| 253 | set to ``True``, the event is emitted to the |
| 254 | client directly, without going through the queue. |
| 255 | This is more efficient, but only works when a |
| 256 | single server process is used. It is recommended |
| 257 | to always leave this parameter with its default |
| 258 | value of ``False``. |
| 259 | |
| 260 | Note: this method is not designed to be used concurrently. If multiple |
| 261 | tasks are emitting at the same time to the same client connection, then |
| 262 | messages composed of multiple packets may end up being sent in an |
| 263 | incorrect sequence. Use standard concurrency solutions (such as a Lock |
| 264 | object) to prevent this situation. |
| 265 | |
| 266 | Note 2: this method is a coroutine. |
| 267 | """ |
| 268 | if to is None and sid is None: |
| 269 | raise ValueError('Cannot use call() to broadcast.') |
| 270 | if not self.async_handlers: |
| 271 | raise RuntimeError( |
| 272 | 'Cannot use call() when async_handlers is False.') |
| 273 | callback_event = self.eio.create_event() |
| 274 | callback_args = [] |
| 275 | |
| 276 | def event_callback(*args): |
| 277 | callback_args.append(args) |
| 278 | callback_event.set() |
| 279 | |
| 280 | await self.emit(event, data=data, room=to or sid, namespace=namespace, |
| 281 | callback=event_callback, ignore_queue=ignore_queue) |
| 282 | try: |
| 283 | await asyncio.wait_for(callback_event.wait(), timeout) |
| 284 | except asyncio.TimeoutError: |