Skip to content

Commit

Permalink
Added callable header feature (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
figaro-smartotum authored Jul 29, 2023
1 parent 3fa4cfc commit 6cdb982
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions websocket/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class WebSocketApp:
Higher level of APIs are provided. The interface is like JavaScript WebSocket object.
"""

def __init__(self, url: str, header: list or dict = None,
def __init__(self, url: str, header: list or dict or Callable = None,
on_open: Callable = None, on_message: Callable = None, on_error: Callable = None,
on_close: Callable = None, on_ping: Callable = None, on_pong: Callable = None,
on_cont_message: Callable = None,
Expand All @@ -154,8 +154,11 @@ def __init__(self, url: str, header: list or dict = None,
----------
url: str
Websocket url.
header: list or dict
header: list or dict or Callable
Custom header for websocket handshake.
If the parameter is a callable object, it is called just before the connection attempt.
The returned dict or list is used as custom header value.
This could be useful in order to properly setup timestamp dependent headers.
on_open: function
Callback object which is called at opening websocket.
on_open has one argument.
Expand Down Expand Up @@ -409,8 +412,11 @@ def setSock(reconnecting: bool = False) -> None:

self.sock.settimeout(getdefaulttimeout())
try:

header = self.header() if callable(self.header) else self.header

self.sock.connect(
self.url, header=self.header, cookie=self.cookie,
self.url, header=header, cookie=self.cookie,
http_proxy_host=http_proxy_host,
http_proxy_port=http_proxy_port, http_no_proxy=http_no_proxy,
http_proxy_auth=http_proxy_auth, http_proxy_timeout=http_proxy_timeout,
Expand Down

0 comments on commit 6cdb982

Please sign in to comment.