This commit is contained in:
2024-12-15 17:09:10 +00:00
parent e6ce63d19c
commit 75ec09e005
2 changed files with 69 additions and 30 deletions

View File

@ -7,21 +7,38 @@ from websockets.sync.server import serve
import websockets
import array
import time
from flask import render_template
api = Flask(__name__)
watchers_last_seen = {}
web_users = {}
stream = False
freeze = False
debug = False
def current_time():
return str(int(time.time()))
@api.route("/", methods=["GET"])
def web():
return render_template('index.html')
@api.route("/off", methods=["GET"])
def streaOff():
global stream
stream=False
return "True"
@api.route("/freeze", methods=["GET"])
def freeze_the_stream():
global freeze
if freeze == True:
freeze=False
return "un-frosen"
else:
freeze=True
return "frosen"
@api.route("/on", methods=["GET"])
def streamOn():
global stream
@ -29,15 +46,14 @@ def streamOn():
return "True"
@api.route("/views", methods=["GET"])
def views():
def return_views():
global watchers_last_seen
return str(len(watchers_last_seen))
return str(len(watchers_last_seen.keys()))
@api.route("/mode", methods=["GET"])
def mode():
global watchers_last_seen
return str(stream)
return "streaming-"+str(stream)+"\n frozen-"+str(freeze)
def ws_server(websocket):
global stream, watchers_last_seen
@ -52,11 +68,16 @@ def ws_server(websocket):
if debug: print(req,"-----",args)
match (req):
case ("get_stream"):
if stream:
watchers_last_seen[args["uuid"]] = current_time()
websocket.send(f""+str(json.dumps([True, screenlive.gen()])))
if freeze == True:
websocket.send(f"freeze")
else:
websocket.send(f"null")
if stream:
watchers_last_seen[args["uuid"]] = current_time()
websocket.send(f""+str(json.dumps([True, screenlive.gen()])))
else:
websocket.send(f"null")
case ("web-site-user"):
users[args["uuid"]] = current_time()
case (_):
websocket.send(f"null")
except websockets.exceptions.ConnectionClosedOK:
@ -76,8 +97,14 @@ def timeout_task():
if debug: print(key+"is timed out")
watchers_last_seen.pop(key)
break
for key,value in users.items():
if debug: print(key+"||||"+value)
if (int(current_time())-int(value)) > 5:
if debug: print(key+"is timed out")
users.pop(key)
break
if debug: print("time -",current_time())
if debug: print("watchers -",watchers_last_seen.keys())
if debug: print("watchers -",watchers_last_seen.keys(),"::::: users - ",users.keys())
sleep(10)
def run_flask_server():