Skip to content

Added args for message_received callback method #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The WebsocketServer can be initialized with the below parameters.
| `set_fn_new_client()` | Sets a callback function that will be called for every new `client` connecting to us | function | None |
| `set_fn_client_left()` | Sets a callback function that will be called for every `client` disconnecting from us | function | None |
| `set_fn_message_received()` | Sets a callback function that will be called when a `client` sends a message | function | None |
| `send_message()` | Sends a `message` to a specific `client`. The message is a simple string. | client, message | None |
| `send_message()` | Sends a `message` to a specific `client`. The message is a simple string. | client, message, args | None |
| `send_message_to_all()` | Sends a `message` to **all** connected clients. The message is a simple string. | message | None |
| `disconnect_clients_gracefully()` | Disconnect all connected clients by sending a websocket CLOSE handshake. | Optional: status, reason | None |
| `disconnect_clients_abruptly()` | Disconnect all connected clients. Clients won't be aware until they try to send some data. | None | None |
Expand Down
7 changes: 4 additions & 3 deletions websocket_server/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def new_client(self, client, server):
def client_left(self, client, server):
pass

def message_received(self, client, server, message):
def message_received(self, client, server, message, args=None):
pass

def set_fn_new_client(self, fn):
Expand All @@ -71,8 +71,9 @@ def set_fn_new_client(self, fn):
def set_fn_client_left(self, fn):
self.client_left = fn

def set_fn_message_received(self, fn):
def set_fn_message_received(self, fn, args=None):
self.message_received = fn
self.message_received_args = args

def send_message(self, client, msg):
self._unicast(client, msg)
Expand Down Expand Up @@ -160,7 +161,7 @@ def _run_forever(self, threaded):
sys.exit(1)

def _message_received_(self, handler, msg):
self.message_received(self.handler_to_client(handler), self, msg)
self.message_received(self.handler_to_client(handler), self, msg, self.message_received_args)

def _ping_received_(self, handler, msg):
handler.send_pong(msg)
Expand Down