@@ -24,6 +24,13 @@ class Server(object):
2424 use. To disable logging set to ``False``. The default is
2525 ``False``. Note that fatal errors are logged even when
2626 ``logger`` is ``False``.
27+ :param serializer: The serialization method to use when transmitting
28+ packets. Valid values are ``'default'``, ``'pickle'``,
29+ ``'msgpack'`` and ``'cbor'``. Alternatively, a subclass
30+ of the :class:`Packet` class with custom implementations
31+ of the ``encode()`` and ``decode()`` methods can be
32+ provided. Client and server must use compatible
33+ serializers.
2734 :param json: An alternative json module to use for encoding and decoding
2835 packets. Custom json modules must have ``dumps`` and ``loads``
2936 functions that are compatible with the standard library
@@ -48,10 +55,11 @@ class Server(object):
4855
4956 :param async_mode: The asynchronous model to use. See the Deployment
5057 section in the documentation for a description of the
51- available options. Valid async modes are "threading",
52- "eventlet", "gevent" and "gevent_uwsgi". If this
53- argument is not given, "eventlet" is tried first, then
54- "gevent_uwsgi", then "gevent", and finally "threading".
58+ available options. Valid async modes are
59+ ``'threading'``, ``'eventlet'``, ``'gevent'`` and
60+ ``'gevent_uwsgi'``. If this argument is not given,
61+ ``'eventlet'`` is tried first, then ``'gevent_uwsgi'``,
62+ then ``'gevent'``, and finally ``'threading'``.
5563 The first async mode that has all its dependencies
5664 installed is then one that is chosen.
5765 :param ping_interval: The interval in seconds at which the server pings
@@ -98,14 +106,22 @@ class Server(object):
98106 fatal errors are logged even when
99107 ``engineio_logger`` is ``False``.
100108 """
101- def __init__ (self , client_manager = None , logger = False , json = None ,
102- async_handlers = True , always_connect = False , ** kwargs ):
109+ def __init__ (self , client_manager = None , logger = False , serializer = 'default' ,
110+ json = None , async_handlers = True , always_connect = False ,
111+ ** kwargs ):
103112 engineio_options = kwargs
104113 engineio_logger = engineio_options .pop ('engineio_logger' , None )
105114 if engineio_logger is not None :
106115 engineio_options ['logger' ] = engineio_logger
116+ if serializer == 'default' :
117+ self .packet_class = packet .Packet
118+ elif serializer == 'msgpack' :
119+ from . import msgpack_packet
120+ self .packet_class = msgpack_packet .MsgPackPacket
121+ else :
122+ self .packet_class = serializer
107123 if json is not None :
108- packet . Packet .json = json
124+ self . packet_class .json = json
109125 engineio_options ['json' ] = json
110126 engineio_options ['async_handlers' ] = False
111127 self .eio = self ._engineio_server_class ()(** engineio_options )
@@ -531,7 +547,7 @@ def disconnect(self, sid, namespace=None, ignore_queue=False):
531547 if delete_it :
532548 self .logger .info ('Disconnecting %s [%s]' , sid , namespace )
533549 eio_sid = self .manager .pre_disconnect (sid , namespace = namespace )
534- self ._send_packet (eio_sid , packet . Packet (
550+ self ._send_packet (eio_sid , self . packet_class (
535551 packet .DISCONNECT , namespace = namespace ))
536552 self ._trigger_event ('disconnect' , namespace , sid )
537553 self .manager .disconnect (sid , namespace = namespace )
@@ -609,7 +625,7 @@ def _emit_internal(self, eio_sid, event, data, namespace=None, id=None):
609625 data = [data ]
610626 else :
611627 data = []
612- self ._send_packet (eio_sid , packet . Packet (
628+ self ._send_packet (eio_sid , self . packet_class (
613629 packet .EVENT , namespace = namespace , data = [event ] + data , id = id ))
614630
615631 def _send_packet (self , eio_sid , pkt ):
@@ -626,7 +642,7 @@ def _handle_connect(self, eio_sid, namespace, data):
626642 namespace = namespace or '/'
627643 sid = self .manager .connect (eio_sid , namespace )
628644 if self .always_connect :
629- self ._send_packet (eio_sid , packet . Packet (
645+ self ._send_packet (eio_sid , self . packet_class (
630646 packet .CONNECT , {'sid' : sid }, namespace = namespace ))
631647 fail_reason = exceptions .ConnectionRefusedError ().error_args
632648 try :
@@ -647,15 +663,15 @@ def _handle_connect(self, eio_sid, namespace, data):
647663 if success is False :
648664 if self .always_connect :
649665 self .manager .pre_disconnect (sid , namespace )
650- self ._send_packet (eio_sid , packet . Packet (
666+ self ._send_packet (eio_sid , self . packet_class (
651667 packet .DISCONNECT , data = fail_reason , namespace = namespace ))
652668 else :
653- self ._send_packet (eio_sid , packet . Packet (
669+ self ._send_packet (eio_sid , self . packet_class (
654670 packet .CONNECT_ERROR , data = fail_reason ,
655671 namespace = namespace ))
656672 self .manager .disconnect (sid , namespace )
657673 elif not self .always_connect :
658- self ._send_packet (eio_sid , packet . Packet (
674+ self ._send_packet (eio_sid , self . packet_class (
659675 packet .CONNECT , {'sid' : sid }, namespace = namespace ))
660676
661677 def _handle_disconnect (self , eio_sid , namespace ):
@@ -697,7 +713,7 @@ def _handle_event_internal(self, server, sid, eio_sid, data, namespace,
697713 data = list (r )
698714 else :
699715 data = [r ]
700- server ._send_packet (eio_sid , packet . Packet (
716+ server ._send_packet (eio_sid , self . packet_class (
701717 packet .ACK , namespace = namespace , id = id , data = data ))
702718
703719 def _handle_ack (self , eio_sid , namespace , id , data ):
@@ -737,7 +753,7 @@ def _handle_eio_message(self, eio_sid, data):
737753 else :
738754 self ._handle_ack (eio_sid , pkt .namespace , pkt .id , pkt .data )
739755 else :
740- pkt = packet . Packet (encoded_packet = data )
756+ pkt = self . packet_class (encoded_packet = data )
741757 if pkt .packet_type == packet .CONNECT :
742758 self ._handle_connect (eio_sid , pkt .namespace , pkt .data )
743759 elif pkt .packet_type == packet .DISCONNECT :
0 commit comments