55from ws4py .framing import Frame , OPCODE_CONTINUATION , OPCODE_TEXT , \
66 OPCODE_BINARY , OPCODE_CLOSE , OPCODE_PING , OPCODE_PONG
77
8- __all__ = ['TextMessage' , 'BinaryMessage' , 'CloseControlMessage' ,
8+ __all__ = ['Message' , ' TextMessage' , 'BinaryMessage' , 'CloseControlMessage' ,
99 'PingControlMessage' , 'PongControlMessage' ]
1010
1111class Message (object ):
12- def __init__ (self , opcode , data = '' , encoding = 'utf-8' , size = None ):
12+ def __init__ (self , opcode , data = '' , encoding = 'utf-8' ):
1313 """
14- A WebSocket message is made of an opcode defining its type
15- and some bytes.
14+ A message is a application level entity. It's usually built
15+ from one or many frames. The protocol defines several kind
16+ of messages which are grouped into two sets:
1617
17- @param opcode: message type
18- @param data: message bytes
19- @param encoding: how to encode the message bytes
18+ * data messages which can be text or binary typed
19+ * control messages which provide a mechanism to perform
20+ in-band control communication between peers
21+
22+ The ``opcode`` indicates the message type and ``data`` is
23+ the possible message payload.
24+
25+ The payload is held internally as a a :func:`bytearray` as they are
26+ faster than pure strings for append operations.
27+
28+ Unicode data will be encoded using the provided ``encoding``.
2029 """
2130 self .opcode = opcode
2231 self ._completed = False
@@ -36,18 +45,23 @@ def __init__(self, opcode, data='', encoding='utf-8', size=None):
3645 def single (self , mask = False ):
3746 """
3847 Returns a frame bytes with the fin bit set and a random mask.
48+
49+ If ``mask`` is set, automatically mask the frame
50+ using a generated 4-byte token.
3951 """
4052 mask = os .urandom (4 ) if mask else None
4153 return Frame (body = self .data or '' , opcode = self .opcode ,
4254 masking_key = mask , fin = 1 ).build ()
4355
4456 def fragment (self , first = False , last = False , mask = False ):
4557 """
46- Returns a frame bytes as part of a fragmented message.
58+ Returns a :class:`ws4py.framing.Frame` bytes.
59+
60+ The behavior depends on the given flags:
4761
48- @param first: indicates this is the first frame of the message
49- @param last: indicates this is the last frame of the message,
50- setting the fin bit
62+ * `` first``: the frame uses ``self.opcode`` else a continuation opcode
63+ * `` last``: the frame has its ``fin`` bit set
64+ * ``mask``: the frame is masked using a automatically generated 4-byte token
5165 """
5266 fin = 1 if last is True else 0
5367 opcode = self .opcode if first is True else OPCODE_CONTINUATION
@@ -60,7 +74,7 @@ def fragment(self, first=False, last=False, mask=False):
6074 def completed (self ):
6175 """
6276 Indicates the the message is complete, meaning
63- the frame fin bit was set for its last frame .
77+ the frame's `` fin`` bit was set.
6478 """
6579 return self ._completed
6680
@@ -74,9 +88,7 @@ def completed(self, state):
7488
7589 def extend (self , data ):
7690 """
77- Add more bytes to the message.
78-
79- @param: bytes to add to this message.
91+ Add more ``data`` to the message.
8092 """
8193 if isinstance (data , unicode ):
8294 data = data .encode (self .encoding )
0 commit comments