Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions websocket_server/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def disconnect_clients_gracefully(self, status=CLOSE_STATUS_NORMAL, reason=DEFAU
def disconnect_clients_abruptly(self):
self._disconnect_clients_abruptly()

def disconnect_client_gracefully(self, client, status=CLOSE_STATUS_NORMAL, reason=DEFAULT_CLOSE_REASON):
self._disconnect_client_gracefully(client, status, reason)

def disconnect_client_abruptly(self, client):
self._disconnect_client_abruptly(client)


class WebsocketServer(ThreadingMixIn, TCPServer, API):
"""
Expand Down Expand Up @@ -247,6 +253,33 @@ def _disconnect_clients_abruptly(self):
"""
self._terminate_client_handlers()

def _disconnect_client_gracefully(self, client, status, reason):
"""
Terminate a client gracefully without shutting down the server
"""
for cl in self.clients:
if cl["id"] == client["id"]:
self._client_left_(client["handler"])
client["handler"].send_close(status, reason)
return

logger.error("Client does not exist/was not found")

def _disconnect_client_abruptly(self, client):
"""
Terminate client abruptly (no CLOSE handshake) without shutting down the server
"""
for cl in self.clients:
if cl["id"] == client["id"]:
self._client_left_(client["handler"])
client["handler"].keep_alive = False
client["handler"].finish()
client["handler"].connection.close()
return

logger.error("Client does not exist/was not found")


def _deny_new_connections(self, status, reason):
self._deny_clients = {
"status": status,
Expand Down