Skip to content

Commit 7286312

Browse files
committed
Cleanup
1 parent b59f0ab commit 7286312

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

websocket_server/websocket_server.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,11 @@ def send_message(self, client, msg):
8686
def send_message_to_all(self, msg):
8787
self._multicast(msg)
8888

89-
def _terminate_client_handlers(self):
90-
"""
91-
Ensures request handler for each client is terminated correctly
92-
"""
93-
for client in self.clients:
94-
client["handler"].keep_alive = False
95-
client["handler"].finish()
96-
client["handler"].connection.close()
97-
9889
def shutdown_gracefully(self, status=CLOSE_STATUS_NORMAL, reason=DEFAULT_CLOSE_REASON):
99-
"""
100-
Send a CLOSE handshake to all connected clients before terminating server
101-
"""
102-
self.keep_alive = False
103-
104-
# Send CLOSE to clients
105-
for client in self.clients:
106-
client["handler"].send_close(CLOSE_STATUS_NORMAL, reason)
107-
108-
self._terminate_client_handlers()
109-
self.server_close()
90+
self._shutdown_gracefully(status=CLOSE_STATUS_NORMAL, reason=DEFAULT_CLOSE_REASON)
11091

11192
def shutdown_abruptly(self):
112-
"""
113-
Terminate server without sending a CLOSE handshake
114-
"""
115-
self.keep_alive = False
116-
self._terminate_client_handlers()
117-
self.server_close()
93+
self._shutdown_abruptly()
11894

11995

12096
class WebsocketServer(ThreadingMixIn, TCPServer, API):
@@ -190,6 +166,36 @@ def handler_to_client(self, handler):
190166
if client['handler'] == handler:
191167
return client
192168

169+
def _terminate_client_handlers(self):
170+
"""
171+
Ensures request handler for each client is terminated correctly
172+
"""
173+
for client in self.clients:
174+
client["handler"].keep_alive = False
175+
client["handler"].finish()
176+
client["handler"].connection.close()
177+
178+
def _shutdown_gracefully(self, status=CLOSE_STATUS_NORMAL, reason=DEFAULT_CLOSE_REASON):
179+
"""
180+
Send a CLOSE handshake to all connected clients before terminating server
181+
"""
182+
self.keep_alive = False
183+
184+
# Send CLOSE to clients
185+
for client in self.clients:
186+
client["handler"].send_close(CLOSE_STATUS_NORMAL, reason)
187+
188+
self._terminate_client_handlers()
189+
self.server_close()
190+
191+
def _shutdown_abruptly(self):
192+
"""
193+
Terminate server without sending a CLOSE handshake
194+
"""
195+
self.keep_alive = False
196+
self._terminate_client_handlers()
197+
self.server_close()
198+
193199

194200
class WebSocketHandler(StreamRequestHandler):
195201

0 commit comments

Comments
 (0)