-
Notifications
You must be signed in to change notification settings - Fork 2
/
coinigy_ws_client.py
68 lines (42 loc) · 1.95 KB
/
coinigy_ws_client.py
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
68
import logging
import json
from socketcc import SocketCC
logging.basicConfig(format="%s(levelname)s:%(message)s", level=logging.DEBUG)
api_credentials = {"apiKey": "YOUR_API_KEY", "apiSecret": "SECRET"}
COINIGY_SC_ENDPOINT = "wss://sc-02.coinigy.com/socketcluster/"
def your_code_starts_here(socket: SocketCC):
# Code for subscription
socket.subscribe('WSTRADE-BTRX--BTC--USDT') # Channel to be subscribed
def channel_message(key, data): # Messages will be received here
print(f"Data {data} from channel {key}")
socket.on_channel('WSTRADE-BTRX--BTC--USDT', channel_message) # This is used for watching messages over channel
# Code for emit
def exchanges_response(event_name, error, data):
print(f"Got ack data {data} and event_name is '{event_name}'")
socket.emit("exchanges", None, exchanges_response)
socket.emit("channels", "OK", exchanges_response)
def on_connect(socket: SocketCC):
logging.info(f"Connected to {socket.url}")
def on_disconnect(socket: SocketCC):
logging.info(f"Disconnected from {socket.url}")
def on_connection_error(socket: SocketCC, error):
logging.error(f"Connection error:{error}")
def on_set_auth(socket: SocketCC, token):
logging.info(f"Received auth token:{token}")
socket.set_auth_token(token)
def on_auth(socket: SocketCC, is_authenticated):
logging.info("Authenticated is " + str(is_authenticated))
def after_authenticated(eventname, error, data):
print("token is " + json.dumps(data, sort_keys=True))
your_code_starts_here(socket)
socket.emit("auth", api_credentials, after_authenticated)
if __name__ == "__main__":
scc = SocketCC(COINIGY_SC_ENDPOINT)
scc.set_basic_listener(on_connect, on_disconnect, on_connection_error)
scc.set_auth_listener(on_set_auth, on_auth)
scc.set_reconnection(False)
scc.connect_thread()
while True:
if input("Enter 'q' to quit:") == "q":
break
scc.disconnect()