forked from massive-com/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets-example.py
More file actions
67 lines (62 loc) · 1.39 KB
/
websockets-example.py
File metadata and controls
67 lines (62 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage, EquityTrade
from typing import List
# import logging
# logging.basicConfig(
# format="%(asctime)s %(message)s",
# level=logging.DEBUG,
# )
c = WebSocketClient(subscriptions=['T.*'])
# Sync
# def handle_msg(msgs: List[WebSocketMessage]):
# for m in msgs:
# print(m)
#
# c.run(handle_msg)
# Sync aggs
# class MessageHandler:
# count = 0
#
# def handle_msg(self, msgs: List[WebSocketMessage]):
# for m in msgs:
# if type(m) == EquityTrade:
# print(self.count, m)
# self.count += 1
#
# h = MessageHandler()
#
# def handle_msg(msgs: List[WebSocketMessage]):
# h.handle_msg(msgs)
#
# c.run(handle_msg)
# Async
# import asyncio
# async def handle_msg(msgs: List[WebSocketMessage]):
# for m in msgs:
# print(m)
#
# async def timeout():
# await asyncio.sleep(1)
# print('unsubscribe_all')
# c.unsubscribe_all()
# await asyncio.sleep(1)
# print('close')
# await c.close()
#
# async def main():
# await asyncio.gather(
# c.connect(handle_msg),
# timeout()
# )
#
# asyncio.run(main())
# Raw
# import json
# from typing import Union
#
# c = WebSocketClient(subscriptions=['T.*'], raw=True)
#
# def handle_msg(msgs: Union[str, bytes]):
# print(json.loads(msgs))
#
# c.run(handle_msg)