-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebSocketClient.py
62 lines (55 loc) · 2.23 KB
/
webSocketClient.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
import websockets
import asyncio
import uuid
import json
class WebSocketClient():
def __init__(self):
# list of topics to subscribe to
self.topics = ["channel-points-channel-v1.<ChannelID>"]
self.auth_token = "<Auth_Token>"
pass
async def connect(self):
'''
Connecting to webSocket server
websockets.client.connect returns a WebSocketClientProtocol, which is used to send and receive messages
'''
self.connection = await websockets.connect('wss://pubsub-edge.twitch.tv')
if self.connection.open:
print('Connection stablished. Client correcly connected')
# Send greeting
message = {"type": "LISTEN", "nonce": str(self.generate_nonce()), "data":{"topics": self.topics, "auth_token": self.auth_token}}
json_message = json.dumps(message)
await self.sendMessage(json_message)
return self.connection
def generate_nonce(self):
'''Generate pseudo-random number and seconds since epoch (UTC).'''
nonce = uuid.uuid1()
oauth_nonce = nonce.hex
return oauth_nonce
async def sendMessage(self, message):
'''Sending message to webSocket server'''
await self.connection.send(message)
async def receiveMessage(self, connection):
'''Receiving all server messages and handling them'''
while True:
try:
message = await connection.recv()
print('Received message from server: ' + str(message))
except websockets.exceptions.ConnectionClosed:
print('Connection with server closed')
break
async def heartbeat(self, connection):
'''
Sending heartbeat to server every 1 minutes
Ping - pong messages to verify/keep connection is alive
'''
while True:
try:
data_set = {"type": "PING"}
json_request = json.dumps(data_set)
print(json_request)
await connection.send(json_request)
await asyncio.sleep(60)
except websockets.exceptions.ConnectionClosed:
print('Connection with server closed')
break