MCPcopy Create free account
hub / github.com/miguelgrinberg/python-socketio / emit

Method emit

src/socketio/manager.py:22–64  ·  view source on GitHub ↗

Emit a message to a single client, a room, or all the clients connected to the namespace.

(self, event, data, namespace, room=None, skip_sid=None,
             callback=None, to=None, **kwargs)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

get_participantsMethod · 0.80
encodeMethod · 0.45
_send_eio_packetMethod · 0.45
_generate_ack_idMethod · 0.45
_send_packetMethod · 0.45

Tested by

no test coverage detected