Browse Source

Expand tuples to multiple arguments, but not lists

pull/22/head
Miguel Grinberg 9 years ago
parent
commit
259f98d3cd
  1. 19
      docs/index.rst
  2. 11
      socketio/server.py
  3. 16
      tests/test_server.py

19
docs/index.rst

@ -99,10 +99,10 @@ environment dictionary. The server can inspect authentication or other headers
to decide if the client is allowed to connect. To reject a client the handler
must return ``False``.
When the client sends an event to the server the appropriate event handler is
invoked with the ``sid`` and the message. The application can define as many
events as needed and associate them with event handlers. An event is defined
simply by a name.
When the client sends an event to the server, the appropriate event handler is
invoked with the ``sid`` and the message, which can be a single or multiple
arguments. The application can define as many events as needed and associate
them with event handlers. An event is defined simply by a name.
When a connection with a client is broken, the ``disconnect`` event is called,
allowing the application to perform cleanup.
@ -136,10 +136,13 @@ that it will lose the ability to address individual clients.
sio.leave_room(sid, data['room'])
The :func:`socketio.Server.emit` method takes an event name, a message payload
of type ``str``, ``bytes``, ``list`` or ``dict``, and the recipient room. To
address an individual client, the ``sid`` of that client should be given as
room (assuming the application did not alter these initial rooms). To address
all connected clients, the ``room`` argument should be omitted.
of type ``str``, ``bytes``, ``list``, ``dict`` or ``tuple``, and the recipient
room. When sending a ``tuple``, the elements in it need to be of any of the
other four allowed types. The elements of the tuple will be passed as multiple
arguments to the client-side callback function. To address an individual
client, the ``sid`` of that client should be given as room (assuming the
application did not alter these initial rooms). To address all connected
clients, the ``room`` argument should be omitted.
::

11
socketio/server.py

@ -311,8 +311,14 @@ class Server(object):
binary = False # pragma: nocover
else:
binary = None
# tuples are expanded to multiple arguments, everything else is sent
# as a single argument
if isinstance(data, tuple):
data = list(data)
else:
data = [data]
self._send_packet(sid, packet.Packet(packet.EVENT, namespace=namespace,
data=[event, data], id=id,
data=[event] + data, id=id,
binary=binary))
def _send_packet(self, sid, pkt):
@ -365,10 +371,9 @@ class Server(object):
r = self._trigger_event(data[0], namespace, sid, *data[1:])
if id is not None:
# send ACK packet with the response returned by the handler
# tuples are expanded as multiple arguments
if isinstance(r, tuple):
data = list(r)
elif isinstance(r, list):
data = r
else:
data = [r]
if six.PY2 and not self.binary:

16
tests/test_server.py

@ -125,6 +125,20 @@ class TestServer(unittest.TestCase):
'2/foo,["my event","my data"]',
binary=False)
def test_emit_internal_with_tuple(self, eio):
s = server.Server()
s._emit_internal('123', 'my event', ('foo', 'bar'), namespace='/foo')
s.eio.send.assert_called_once_with('123',
'2/foo,["my event","foo","bar"]',
binary=False)
def test_emit_internal_with_list(self, eio):
s = server.Server()
s._emit_internal('123', 'my event', ['foo', 'bar'], namespace='/foo')
s.eio.send.assert_called_once_with('123',
'2/foo,["my event",["foo","bar"]]',
binary=False)
def test_emit_internal_with_callback(self, eio):
s = server.Server()
id = s.manager._generate_ack_id('123', '/foo', 'cb')
@ -304,7 +318,7 @@ class TestServer(unittest.TestCase):
s.on('my message', handler)
s._handle_eio_message('123', '21000["my message","a","b","c"]')
handler.assert_called_once_with('123', 'a', 'b', 'c')
s.eio.send.assert_called_once_with('123', '31000[1,"2",true]',
s.eio.send.assert_called_once_with('123', '31000[[1,"2",true]]',
binary=False)
def test_handle_event_with_ack_binary(self, eio):

Loading…
Cancel
Save