-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketServer.py
More file actions
39 lines (32 loc) · 1.31 KB
/
WebSocketServer.py
File metadata and controls
39 lines (32 loc) · 1.31 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
# Importing necessary libraries
import asyncio
import websockets
import base64
import json
from config import *
# Setting up logging configuration
LOG_FORMAT = '%(asctime)s %(message)s'
# Async function to handle WebSocket communication
async def handle_websocket(websocket, path):
global token
# Continuously listen for new messages from the WebSocket
async for message in websocket:
# Log the received message
# Update the token with the received message
token = message
# Write the new token to a file
with open(SCRIPT_PATH + "/token", "w") as token_file:
token_file.write(token)
# Decode and load the token to extract expiration timestamp
key_info = json.loads(base64.b64decode(token.split('.')[1] + '==').decode('utf-8'))
exp_timestamp = key_info['exp']
# Write the expiration timestamp to a file
with open(SCRIPT_PATH + "/token_Exp", "w") as exp_file:
exp_file.write(str(exp_timestamp))
# Async function to start the WebSocket server
async def start_websocket_server():
# Create a WebSocket server and run it indefinitely
async with websockets.serve(handle_websocket, IP_ADDRESS, 1234):
await asyncio.Future() # run forever
# Run the WebSocket server
asyncio.run(start_websocket_server())