Skip to content

Commit 8a4e5ff

Browse files
added "to" parameter as an alias to "room"
1 parent eecd367 commit 8a4e5ff

File tree

4 files changed

+85
-43
lines changed

4 files changed

+85
-43
lines changed

socketio/asyncio_server.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def attach(self, app, socketio_path='socket.io'):
7575
"""Attach the Socket.IO server to an application."""
7676
self.eio.attach(app, socketio_path)
7777

78-
async def emit(self, event, data=None, room=None, skip_sid=None,
78+
async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
7979
namespace=None, callback=None, **kwargs):
8080
"""Emit a custom event to one or more connected clients.
8181
@@ -85,11 +85,12 @@ async def emit(self, event, data=None, room=None, skip_sid=None,
8585
:param data: The data to send to the client or clients. Data can be of
8686
type ``str``, ``bytes``, ``list`` or ``dict``. If a
8787
``list`` or ``dict``, the data will be serialized as JSON.
88-
:param room: The recipient of the message. This can be set to the
89-
session ID of a client to address that client's room, or
90-
to any custom room created by the application, If this
91-
argument is omitted the event is broadcasted to all
92-
connected clients.
88+
:param to: The recipient of the message. This can be set to the
89+
session ID of a client to address only that client, or to
90+
to any custom room created by the application to address all
91+
the clients in that room, If this argument is omitted the
92+
event is broadcasted to all connected clients.
93+
:param room: Alias for the ``to`` parameter.
9394
:param skip_sid: The session ID of a client to skip when broadcasting
9495
to a room or to all clients. This can be used to
9596
prevent a message from being sent to the sender.
@@ -112,14 +113,15 @@ async def emit(self, event, data=None, room=None, skip_sid=None,
112113
Note: this method is a coroutine.
113114
"""
114115
namespace = namespace or '/'
116+
room = to or room
115117
self.logger.info('emitting event "%s" to %s [%s]', event,
116118
room or 'all', namespace)
117119
await self.manager.emit(event, data, namespace, room=room,
118120
skip_sid=skip_sid, callback=callback,
119121
**kwargs)
120122

121-
async def send(self, data, room=None, skip_sid=None, namespace=None,
122-
callback=None, **kwargs):
123+
async def send(self, data, to=None, room=None, skip_sid=None,
124+
namespace=None, callback=None, **kwargs):
123125
"""Send a message to one or more connected clients.
124126
125127
This function emits an event with the name ``'message'``. Use
@@ -128,11 +130,12 @@ async def send(self, data, room=None, skip_sid=None, namespace=None,
128130
:param data: The data to send to the client or clients. Data can be of
129131
type ``str``, ``bytes``, ``list`` or ``dict``. If a
130132
``list`` or ``dict``, the data will be serialized as JSON.
131-
:param room: The recipient of the message. This can be set to the
132-
session ID of a client to address that client's room, or
133-
to any custom room created by the application, If this
134-
argument is omitted the event is broadcasted to all
135-
connected clients.
133+
:param to: The recipient of the message. This can be set to the
134+
session ID of a client to address only that client, or to
135+
to any custom room created by the application to address all
136+
the clients in that room, If this argument is omitted the
137+
event is broadcasted to all connected clients.
138+
:param room: Alias for the ``to`` parameter.
136139
:param skip_sid: The session ID of a client to skip when broadcasting
137140
to a room or to all clients. This can be used to
138141
prevent a message from being sent to the sender.
@@ -154,10 +157,11 @@ async def send(self, data, room=None, skip_sid=None, namespace=None,
154157
155158
Note: this method is a coroutine.
156159
"""
157-
await self.emit('message', data=data, room=room, skip_sid=skip_sid,
158-
namespace=namespace, callback=callback, **kwargs)
160+
await self.emit('message', data=data, to=to, room=room,
161+
skip_sid=skip_sid, namespace=namespace,
162+
callback=callback, **kwargs)
159163

160-
async def call(self, event, data=None, sid=None, namespace=None,
164+
async def call(self, event, data=None, to=None, sid=None, namespace=None,
161165
timeout=60, **kwargs):
162166
"""Emit a custom event to a client and wait for the response.
163167
@@ -167,7 +171,8 @@ async def call(self, event, data=None, sid=None, namespace=None,
167171
:param data: The data to send to the client or clients. Data can be of
168172
type ``str``, ``bytes``, ``list`` or ``dict``. If a
169173
``list`` or ``dict``, the data will be serialized as JSON.
170-
:param sid: The session ID of the recipient client.
174+
:param to: The session ID of the recipient client.
175+
:param sid: Alias for the ``to`` parameter.
171176
:param namespace: The Socket.IO namespace for the event. If this
172177
argument is omitted the event is emitted to the
173178
default namespace.
@@ -192,7 +197,7 @@ def event_callback(*args):
192197
callback_args.append(args)
193198
callback_event.set()
194199

195-
await self.emit(event, data=data, room=sid, namespace=namespace,
200+
await self.emit(event, data=data, room=to or sid, namespace=namespace,
196201
callback=event_callback, **kwargs)
197202
try:
198203
await asyncio.wait_for(callback_event.wait(), timeout)

socketio/server.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ def register_namespace(self, namespace_handler):
236236
self.namespace_handlers[namespace_handler.namespace] = \
237237
namespace_handler
238238

239-
def emit(self, event, data=None, room=None, skip_sid=None, namespace=None,
240-
callback=None, **kwargs):
239+
def emit(self, event, data=None, to=None, room=None, skip_sid=None,
240+
namespace=None, callback=None, **kwargs):
241241
"""Emit a custom event to one or more connected clients.
242242
243243
:param event: The event name. It can be any string. The event names
@@ -246,11 +246,12 @@ def emit(self, event, data=None, room=None, skip_sid=None, namespace=None,
246246
:param data: The data to send to the client or clients. Data can be of
247247
type ``str``, ``bytes``, ``list`` or ``dict``. If a
248248
``list`` or ``dict``, the data will be serialized as JSON.
249-
:param room: The recipient of the message. This can be set to the
250-
session ID of a client to address that client's room, or
251-
to any custom room created by the application, If this
252-
argument is omitted the event is broadcasted to all
253-
connected clients.
249+
:param to: The recipient of the message. This can be set to the
250+
session ID of a client to address only that client, or to
251+
to any custom room created by the application to address all
252+
the clients in that room, If this argument is omitted the
253+
event is broadcasted to all connected clients.
254+
:param room: Alias for the ``to`` parameter.
254255
:param skip_sid: The session ID of a client to skip when broadcasting
255256
to a room or to all clients. This can be used to
256257
prevent a message from being sent to the sender. To
@@ -272,12 +273,13 @@ def emit(self, event, data=None, room=None, skip_sid=None, namespace=None,
272273
value of ``False``.
273274
"""
274275
namespace = namespace or '/'
276+
room = to or room
275277
self.logger.info('emitting event "%s" to %s [%s]', event,
276278
room or 'all', namespace)
277279
self.manager.emit(event, data, namespace, room=room,
278280
skip_sid=skip_sid, callback=callback, **kwargs)
279281

280-
def send(self, data, room=None, skip_sid=None, namespace=None,
282+
def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
281283
callback=None, **kwargs):
282284
"""Send a message to one or more connected clients.
283285
@@ -287,11 +289,12 @@ def send(self, data, room=None, skip_sid=None, namespace=None,
287289
:param data: The data to send to the client or clients. Data can be of
288290
type ``str``, ``bytes``, ``list`` or ``dict``. If a
289291
``list`` or ``dict``, the data will be serialized as JSON.
290-
:param room: The recipient of the message. This can be set to the
291-
session ID of a client to address that client's room, or
292-
to any custom room created by the application, If this
293-
argument is omitted the event is broadcasted to all
294-
connected clients.
292+
:param to: The recipient of the message. This can be set to the
293+
session ID of a client to address only that client, or to
294+
to any custom room created by the application to address all
295+
the clients in that room, If this argument is omitted the
296+
event is broadcasted to all connected clients.
297+
:param room: Alias for the ``to`` parameter.
295298
:param skip_sid: The session ID of a client to skip when broadcasting
296299
to a room or to all clients. This can be used to
297300
prevent a message from being sent to the sender. To
@@ -312,11 +315,11 @@ def send(self, data, room=None, skip_sid=None, namespace=None,
312315
to always leave this parameter with its default
313316
value of ``False``.
314317
"""
315-
self.emit('message', data=data, room=room, skip_sid=skip_sid,
318+
self.emit('message', data=data, to=to, room=room, skip_sid=skip_sid,
316319
namespace=namespace, callback=callback, **kwargs)
317320

318-
def call(self, event, data=None, sid=None, namespace=None, timeout=60,
319-
**kwargs):
321+
def call(self, event, data=None, to=None, sid=None, namespace=None,
322+
timeout=60, **kwargs):
320323
"""Emit a custom event to a client and wait for the response.
321324
322325
:param event: The event name. It can be any string. The event names
@@ -325,7 +328,8 @@ def call(self, event, data=None, sid=None, namespace=None, timeout=60,
325328
:param data: The data to send to the client or clients. Data can be of
326329
type ``str``, ``bytes``, ``list`` or ``dict``. If a
327330
``list`` or ``dict``, the data will be serialized as JSON.
328-
:param sid: The session ID of the recipient client.
331+
:param to: The session ID of the recipient client.
332+
:param sid: Alias for the ``to`` parameter.
329333
:param namespace: The Socket.IO namespace for the event. If this
330334
argument is omitted the event is emitted to the
331335
default namespace.
@@ -350,7 +354,7 @@ def event_callback(*args):
350354
callback_args.append(args)
351355
callback_event.set()
352356

353-
self.emit(event, data=data, room=sid, namespace=namespace,
357+
self.emit(event, data=data, room=to or sid, namespace=namespace,
354358
callback=event_callback, **kwargs)
355359
if not callback_event.wait(timeout=timeout):
356360
raise exceptions.TimeoutError()

tests/asyncio/test_asyncio_server.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,44 @@ def bar():
8484
def test_emit(self, eio):
8585
mgr = self._get_mock_manager()
8686
s = asyncio_server.AsyncServer(client_manager=mgr)
87-
_run(s.emit('my event', {'foo': 'bar'}, room='room',
87+
_run(s.emit('my event', {'foo': 'bar'}, to='room',
8888
skip_sid='123', namespace='/foo', callback='cb'))
8989
s.manager.emit.mock.assert_called_once_with(
9090
'my event', {'foo': 'bar'}, '/foo', room='room', skip_sid='123',
9191
callback='cb')
92+
_run(s.emit('my event', {'foo': 'bar'}, room='room',
93+
skip_sid='123', namespace='/foo', callback='cb'))
94+
s.manager.emit.mock.assert_called_with(
95+
'my event', {'foo': 'bar'}, '/foo', room='room', skip_sid='123',
96+
callback='cb')
9297

9398
def test_emit_default_namespace(self, eio):
9499
mgr = self._get_mock_manager()
95100
s = asyncio_server.AsyncServer(client_manager=mgr)
96-
_run(s.emit('my event', {'foo': 'bar'}, room='room',
101+
_run(s.emit('my event', {'foo': 'bar'}, to='room',
97102
skip_sid='123', callback='cb'))
98103
s.manager.emit.mock.assert_called_once_with(
99104
'my event', {'foo': 'bar'}, '/', room='room', skip_sid='123',
100105
callback='cb')
106+
_run(s.emit('my event', {'foo': 'bar'}, room='room',
107+
skip_sid='123', callback='cb'))
108+
s.manager.emit.mock.assert_called_with(
109+
'my event', {'foo': 'bar'}, '/', room='room', skip_sid='123',
110+
callback='cb')
101111

102112
def test_send(self, eio):
103113
mgr = self._get_mock_manager()
104114
s = asyncio_server.AsyncServer(client_manager=mgr)
105-
_run(s.send('foo', 'room', '123', namespace='/foo', callback='cb'))
115+
_run(s.send('foo', to='room', skip_sid='123', namespace='/foo',
116+
callback='cb'))
106117
s.manager.emit.mock.assert_called_once_with(
107118
'message', 'foo', '/foo', room='room', skip_sid='123',
108119
callback='cb')
120+
_run(s.send('foo', room='room', skip_sid='123', namespace='/foo',
121+
callback='cb'))
122+
s.manager.emit.mock.assert_called_with(
123+
'message', 'foo', '/foo', room='room', skip_sid='123',
124+
callback='cb')
109125

110126
def test_call(self, eio):
111127
mgr = self._get_mock_manager()

tests/common/test_server.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,44 @@ def disconnect():
7575
def test_emit(self, eio):
7676
mgr = mock.MagicMock()
7777
s = server.Server(client_manager=mgr)
78-
s.emit('my event', {'foo': 'bar'}, 'room', '123', namespace='/foo',
79-
callback='cb')
78+
s.emit('my event', {'foo': 'bar'}, to='room', skip_sid='123',
79+
namespace='/foo', callback='cb')
8080
s.manager.emit.assert_called_once_with(
8181
'my event', {'foo': 'bar'}, '/foo', room='room', skip_sid='123',
8282
callback='cb')
83+
s.emit('my event', {'foo': 'bar'}, room='room', skip_sid='123',
84+
namespace='/foo', callback='cb')
85+
s.manager.emit.assert_called_with(
86+
'my event', {'foo': 'bar'}, '/foo', room='room', skip_sid='123',
87+
callback='cb')
8388

8489
def test_emit_default_namespace(self, eio):
8590
mgr = mock.MagicMock()
8691
s = server.Server(client_manager=mgr)
87-
s.emit('my event', {'foo': 'bar'}, 'room', '123', callback='cb')
92+
s.emit('my event', {'foo': 'bar'}, to='room', skip_sid='123',
93+
callback='cb')
8894
s.manager.emit.assert_called_once_with(
8995
'my event', {'foo': 'bar'}, '/', room='room', skip_sid='123',
9096
callback='cb')
97+
s.emit('my event', {'foo': 'bar'}, room='room', skip_sid='123',
98+
callback='cb')
99+
s.manager.emit.assert_called_with(
100+
'my event', {'foo': 'bar'}, '/', room='room', skip_sid='123',
101+
callback='cb')
91102

92103
def test_send(self, eio):
93104
mgr = mock.MagicMock()
94105
s = server.Server(client_manager=mgr)
95-
s.send('foo', 'room', '123', namespace='/foo', callback='cb')
106+
s.send('foo', to='room', skip_sid='123', namespace='/foo',
107+
callback='cb')
96108
s.manager.emit.assert_called_once_with(
97109
'message', 'foo', '/foo', room='room', skip_sid='123',
98110
callback='cb')
111+
s.send('foo', room='room', skip_sid='123', namespace='/foo',
112+
callback='cb')
113+
s.manager.emit.assert_called_with(
114+
'message', 'foo', '/foo', room='room', skip_sid='123',
115+
callback='cb')
99116

100117
def test_call(self, eio):
101118
mgr = mock.MagicMock()

0 commit comments

Comments
 (0)