141 lines
4.7 KiB
Python
141 lines
4.7 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
monday, 23rd Dec., 2024
|
|
|
|
OBJECTIVE:
|
|
|
|
To provide a SocketIO app for socket-base communication with the front-end.
|
|
|
|
REFERENCES:
|
|
|
|
01. YouTube: https://www.youtube.com/watch?v=H1eLJMC5oTg&t=3s
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# System-level activities:
|
|
import io
|
|
import os
|
|
|
|
# my utils:
|
|
from utils_v2.string import json
|
|
|
|
# To work with SocketIO:
|
|
import socketio
|
|
from aiohttp import web
|
|
|
|
# For asynchronous activities:
|
|
import asyncio
|
|
|
|
# for debugging:
|
|
from icecream import IceCreamDebugger
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# The SocketIo server:
|
|
sio = socketio.AsyncServer(cors_allowed_origins = "*")
|
|
app = web.Application()
|
|
sio.attach(app)
|
|
|
|
# Debugging:
|
|
printer = IceCreamDebugger(prefix = "SocketIO | ", includeContext = True)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
@sio.event
|
|
async def connect(sid, environ):
|
|
printer(sid)
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
@sio.event
|
|
async def disconnect(sid):
|
|
printer(sid)
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
async def init():
|
|
|
|
"""
|
|
Initialize stuff here.
|
|
:return: ?
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
async def main():
|
|
|
|
# Start receiving live market data in the background:
|
|
# asyncio.create_task(start_live_feed())
|
|
|
|
# Run the web server:
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, "0.0.0.0", 5214)
|
|
printer("Server running.")
|
|
await site.start()
|
|
|
|
# Keep the server running:
|
|
while True: await asyncio.sleep(3_600)
|
|
|
|
# Let's go:
|
|
asyncio.run(main())
|