Skip to content

Commit b7238ce

Browse files
committed
Added example and made into proper package
1 parent eff77b9 commit b7238ce

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .websocket_client import WebSocketClient, STOCKS_CLUSTER, FOREX_CLUSTER, CRYPTO_CLUSTER
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class WebSocketClient(object):
1515
# TODO: Either an instance of the client couples 1:1 with the cluster or an instance of the Client couples 1:3 with
1616
# the 3 possible clusters (I think I like client per, but then a problem is the user can make multiple clients for
1717
# the same cluster and that's not desirable behavior,
18-
# somehow keeping track with multiple Client instances will be the issue)
18+
# somehow keeping track with multiple Client instances will be the difficulty)
1919
def __init__(self, cluster: str, auth_key: str, process_message: Optional[Callable[[str], None]] = None):
2020
self._host = self.DEFAULT_HOST
2121
self.url = f"wss://{self._host}/{cluster}"
22-
self.auth_key = auth_key
23-
self.ws: websocket.WebSocketApp = websocket.WebSocketApp(self.url, on_error=self._default_on_error,
22+
self.ws: websocket.WebSocketApp = websocket.WebSocketApp(self.url, on_open=self._default_on_open(),
2423
on_close=self._default_on_close,
25-
on_message=self._default_on_message(),
26-
on_open=self._authenticate())
24+
on_error=self._default_on_error,
25+
on_message=self._default_on_message())
26+
self.auth_key = auth_key
2727

2828
self.process_message = process_message
2929

example.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
3+
from client import WebSocketClient, STOCKS_CLUSTER
4+
5+
6+
def my_customer_process_message(message):
7+
print("this is my custom message processing", message)
8+
9+
10+
def main():
11+
key = 'your api key'
12+
my_client = WebSocketClient(STOCKS_CLUSTER, key, my_customer_process_message)
13+
my_client.run_async()
14+
15+
my_client.subscribe("T.MSFT", "T.AAPL", "T.AMD", "T.NVDA")
16+
time.sleep(1)
17+
18+
my_client.close_connection()
19+
20+
21+
if __name__ == "__main__":
22+
main()

0 commit comments

Comments
 (0)