Skip to content

Commit 58e0ada

Browse files
committed
docstrings
1 parent e796961 commit 58e0ada

1 file changed

Lines changed: 59 additions & 13 deletions

File tree

ws4py/websocket.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from sys import exc_info
88
import traceback
99
import types
10-
import time
1110

1211
from ws4py import WS_KEY
1312
from ws4py.exc import HandshakeError, StreamClosed
@@ -20,37 +19,70 @@
2019
__all__ = ['WebSocket', 'EchoWebSocket']
2120

2221
class WebSocket(object):
23-
def __init__(self, sock, protocols, extensions):
22+
def __init__(self, sock, protocols, extensions, environ=None):
2423
"""
25-
A handler appropriate for servers. This handler
26-
runs the connection's read and parsing in a thread,
27-
meaning that incoming messages will be alerted in a different
28-
thread from the one that created the handler.
24+
Represents a websocket endpoint and provides a high level
25+
interface to drive the endpoint.
2926
3027
@param sock: opened connection after the websocket upgrade
3128
@param protocols: list of protocols from the handshake
3229
@param extensions: list of extensions from the handshake
30+
@param environ: WSGI environ dictionary coming from the
31+
underlying WSGI HTTP server, providing the context for
32+
this connection.
3333
"""
34+
3435
self.stream = Stream(always_mask=False)
36+
"""
37+
Underlying websocket stream that performs the websocket
38+
parsing to high level objects. By default this stream
39+
never masks its messages. Clients using this class should
40+
set the `stream.always_mask` field to `True`.
41+
"""
3542

3643
self.protocols = protocols
44+
"""
45+
List of protocols supported by this endpoint.
46+
Unused for now.
47+
"""
48+
3749
self.extensions = extensions
50+
"""
51+
List of extensions supported by this endpoint.
52+
Unused for now.
53+
"""
3854

3955
self.sock = sock
56+
"""
57+
Underlying connection.
58+
"""
4059

4160
self.client_terminated = False
61+
"""
62+
Indicates if the client has been marked as terminated.
63+
"""
64+
4265
self.server_terminated = False
66+
"""
67+
Indicates if the server has been marked as terminated.
68+
"""
4369

4470
self.reading_buffer_size = DEFAULT_READING_SIZE
71+
"""
72+
Current connection reading buffer size.
73+
"""
4574

4675
self.sender = self.sock.sendall
4776

77+
self.environ = environ
78+
"""
79+
WSGI environ dictionary.
80+
"""
81+
4882
def opened(self):
4983
"""
5084
Called by the server when the upgrade handshake
51-
has succeeeded. Starts the internal loop that
52-
reads bytes from the connection and hands it over
53-
to the stream for parsing.
85+
has succeeeded.
5486
"""
5587
pass
5688

@@ -59,7 +91,7 @@ def close(self, code=1000, reason=''):
5991
Call this method to initiate the websocket connection
6092
closing by sending a close frame to the connected peer.
6193
62-
Once this method is called, the server_terminated
94+
Once this method is called, the `server_terminated`
6395
attribute is set. Calling this method several times is
6496
safe as the closing frame will be sent only the first
6597
time.
@@ -108,6 +140,15 @@ def ponged(self, pong):
108140
pass
109141

110142
def received_message(self, message):
143+
"""
144+
Called whenever a complete message, binary or text,
145+
is received and ready for application's processing.
146+
147+
You should override this methid in your subclass.
148+
149+
@param message: messaging.TextMessage or
150+
messaging.BinaryMessage instance
151+
"""
111152
pass
112153

113154
def send(self, payload, binary=False):
@@ -142,6 +183,9 @@ def send(self, payload, binary=False):
142183
self.sender(message_sender(bytes).fragment(last=True))
143184

144185
def _cleanup(self):
186+
"""
187+
Frees up some resources used by the endpoint.
188+
"""
145189
self.sender = None
146190
self.stream.release()
147191
self.stream = None
@@ -165,19 +209,21 @@ def run(self):
165209
* Whenever an error is raised by the stream parsing,
166210
we initiate the closing of the connection with the
167211
appropiate error code.
212+
213+
This method is blocking and should likely be run
214+
in a thread.
168215
"""
169216
self.sock.setblocking(True)
170217
self.opened()
171218
try:
172219
s = self.stream
173220
sock = self.sock
174221
fileno = sock.fileno()
175-
176222
while not self.terminated:
177223
bytes = sock.recv(self.reading_buffer_size)
178224
if not bytes and self.reading_buffer_size > 0:
179225
break
180-
226+
181227
self.reading_buffer_size = s.parser.send(bytes) or DEFAULT_READING_SIZE
182228

183229
if s.closing is not None:
@@ -220,5 +266,5 @@ def run(self):
220266

221267
class EchoWebSocket(WebSocket):
222268
def received_message(self, message):
223-
self.send(message, message.is_binary)
269+
self.send(message.data, message.is_binary)
224270

0 commit comments

Comments
 (0)