@@ -24,6 +24,13 @@ class Server(object):
24
24
use. To disable logging set to ``False``. The default is
25
25
``False``. Note that fatal errors are logged even when
26
26
``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.
27
34
:param json: An alternative json module to use for encoding and decoding
28
35
packets. Custom json modules must have ``dumps`` and ``loads``
29
36
functions that are compatible with the standard library
@@ -48,10 +55,11 @@ class Server(object):
48
55
49
56
:param async_mode: The asynchronous model to use. See the Deployment
50
57
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'``.
55
63
The first async mode that has all its dependencies
56
64
installed is then one that is chosen.
57
65
:param ping_interval: The interval in seconds at which the server pings
@@ -98,14 +106,22 @@ class Server(object):
98
106
fatal errors are logged even when
99
107
``engineio_logger`` is ``False``.
100
108
"""
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 ):
103
112
engineio_options = kwargs
104
113
engineio_logger = engineio_options .pop ('engineio_logger' , None )
105
114
if engineio_logger is not None :
106
115
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
107
123
if json is not None :
108
- packet . Packet .json = json
124
+ self . packet_class .json = json
109
125
engineio_options ['json' ] = json
110
126
engineio_options ['async_handlers' ] = False
111
127
self .eio = self ._engineio_server_class ()(** engineio_options )
@@ -531,7 +547,7 @@ def disconnect(self, sid, namespace=None, ignore_queue=False):
531
547
if delete_it :
532
548
self .logger .info ('Disconnecting %s [%s]' , sid , namespace )
533
549
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 (
535
551
packet .DISCONNECT , namespace = namespace ))
536
552
self ._trigger_event ('disconnect' , namespace , sid )
537
553
self .manager .disconnect (sid , namespace = namespace )
@@ -609,7 +625,7 @@ def _emit_internal(self, eio_sid, event, data, namespace=None, id=None):
609
625
data = [data ]
610
626
else :
611
627
data = []
612
- self ._send_packet (eio_sid , packet . Packet (
628
+ self ._send_packet (eio_sid , self . packet_class (
613
629
packet .EVENT , namespace = namespace , data = [event ] + data , id = id ))
614
630
615
631
def _send_packet (self , eio_sid , pkt ):
@@ -626,7 +642,7 @@ def _handle_connect(self, eio_sid, namespace, data):
626
642
namespace = namespace or '/'
627
643
sid = self .manager .connect (eio_sid , namespace )
628
644
if self .always_connect :
629
- self ._send_packet (eio_sid , packet . Packet (
645
+ self ._send_packet (eio_sid , self . packet_class (
630
646
packet .CONNECT , {'sid' : sid }, namespace = namespace ))
631
647
fail_reason = exceptions .ConnectionRefusedError ().error_args
632
648
try :
@@ -647,15 +663,15 @@ def _handle_connect(self, eio_sid, namespace, data):
647
663
if success is False :
648
664
if self .always_connect :
649
665
self .manager .pre_disconnect (sid , namespace )
650
- self ._send_packet (eio_sid , packet . Packet (
666
+ self ._send_packet (eio_sid , self . packet_class (
651
667
packet .DISCONNECT , data = fail_reason , namespace = namespace ))
652
668
else :
653
- self ._send_packet (eio_sid , packet . Packet (
669
+ self ._send_packet (eio_sid , self . packet_class (
654
670
packet .CONNECT_ERROR , data = fail_reason ,
655
671
namespace = namespace ))
656
672
self .manager .disconnect (sid , namespace )
657
673
elif not self .always_connect :
658
- self ._send_packet (eio_sid , packet . Packet (
674
+ self ._send_packet (eio_sid , self . packet_class (
659
675
packet .CONNECT , {'sid' : sid }, namespace = namespace ))
660
676
661
677
def _handle_disconnect (self , eio_sid , namespace ):
@@ -697,7 +713,7 @@ def _handle_event_internal(self, server, sid, eio_sid, data, namespace,
697
713
data = list (r )
698
714
else :
699
715
data = [r ]
700
- server ._send_packet (eio_sid , packet . Packet (
716
+ server ._send_packet (eio_sid , self . packet_class (
701
717
packet .ACK , namespace = namespace , id = id , data = data ))
702
718
703
719
def _handle_ack (self , eio_sid , namespace , id , data ):
@@ -737,7 +753,7 @@ def _handle_eio_message(self, eio_sid, data):
737
753
else :
738
754
self ._handle_ack (eio_sid , pkt .namespace , pkt .id , pkt .data )
739
755
else :
740
- pkt = packet . Packet (encoded_packet = data )
756
+ pkt = self . packet_class (encoded_packet = data )
741
757
if pkt .packet_type == packet .CONNECT :
742
758
self ._handle_connect (eio_sid , pkt .namespace , pkt .data )
743
759
elif pkt .packet_type == packet .DISCONNECT :
0 commit comments