Emit a message to a single client, a room, or all the clients connected to the namespace. Note: this method is a coroutine.
(self, event, data, namespace, room=None, skip_sid=None,
callback=None, to=None, **kwargs)
| 12 | return self.is_connected(sid, namespace) |
| 13 | |
| 14 | async def emit(self, event, data, namespace, room=None, skip_sid=None, |
| 15 | callback=None, to=None, **kwargs): |
| 16 | """Emit a message to a single client, a room, or all the clients |
| 17 | connected to the namespace. |
| 18 | |
| 19 | Note: this method is a coroutine. |
| 20 | """ |
| 21 | room = to or room |
| 22 | if namespace not in self.rooms: |
| 23 | return |
| 24 | if isinstance(data, tuple): |
| 25 | # tuples are expanded to multiple arguments, everything else is |
| 26 | # sent as a single argument |
| 27 | data = list(data) |
| 28 | elif data is not None: |
| 29 | data = [data] |
| 30 | else: |
| 31 | data = [] |
| 32 | if not isinstance(skip_sid, list): |
| 33 | skip_sid = [skip_sid] |
| 34 | tasks = [] |
| 35 | if not callback: |
| 36 | # when callbacks aren't used the packets sent to each recipient are |
| 37 | # identical, so they can be generated once and reused |
| 38 | pkt = self.server.packet_class( |
| 39 | packet.EVENT, namespace=namespace, data=[event] + data) |
| 40 | encoded_packet = pkt.encode() |
| 41 | if not isinstance(encoded_packet, list): |
| 42 | encoded_packet = [encoded_packet] |
| 43 | eio_pkt = [eio_packet.Packet(eio_packet.MESSAGE, p) |
| 44 | for p in encoded_packet] |
| 45 | for sid, eio_sid in self.get_participants(namespace, room): |
| 46 | if sid not in skip_sid: |
| 47 | for p in eio_pkt: |
| 48 | tasks.append(asyncio.create_task( |
| 49 | self.server._send_eio_packet(eio_sid, p))) |
| 50 | else: |
| 51 | # callbacks are used, so each recipient must be sent a packet that |
| 52 | # contains a unique callback id |
| 53 | # note that callbacks when addressing a group of people are |
| 54 | # implemented but not tested or supported |
| 55 | for sid, eio_sid in self.get_participants(namespace, room): |
| 56 | if sid not in skip_sid: # pragma: no branch |
| 57 | id = self._generate_ack_id(sid, callback) |
| 58 | pkt = self.server.packet_class( |
| 59 | packet.EVENT, namespace=namespace, data=[event] + data, |
| 60 | id=id) |
| 61 | tasks.append(asyncio.create_task( |
| 62 | self.server._send_packet(eio_sid, pkt))) |
| 63 | if tasks == []: # pragma: no cover |
| 64 | return |
| 65 | await asyncio.wait(tasks) |
| 66 | |
| 67 | async def connect(self, eio_sid, namespace): |
| 68 | """Register a client connection to a namespace. |
nothing calls this directly
no test coverage detected