webug/websocket.py

49 lines
1.2 KiB
Python
Raw Normal View History

2019-12-14 19:07:29 +01:00
import json
2020-02-21 15:45:30 +01:00
from flask import request
2019-12-14 19:07:29 +01:00
from flask_socketio import emit
2020-02-21 15:45:30 +01:00
from objects.display import Display, Window, Tab
2019-12-14 19:07:29 +01:00
from objects import glob
2020-02-21 15:45:30 +01:00
from helpers import uuid, connection
@glob.websocket.on_error_default # handles all namespaces without an explicit error handler
def default_error_handler(e):
print("ERR:", e)
return 500
2019-12-14 19:07:29 +01:00
@glob.websocket.on("connect")
def ws_connect():
2020-02-21 15:45:30 +01:00
connection.connect(request.sid)
return "hi"
@glob.websocket.on("disconnect")
def ws_disconnect():
connection.disconnect(request.sid)
2019-12-14 19:07:29 +01:00
@glob.websocket.on("display")
def ws_display(data):
def handle_list(_):
2020-02-21 15:45:30 +01:00
return [ item.serialize() for item in glob.displays.values() ]
def handle_new(_):
display = Display.generate()
return display.serialize()
def handle_connect(data):
uuid = data["uuid"]
if uuid not in glob.displays.keys():
raise Exception("Display not found")
glob.displays[uuid].attach(request.sid)
#print(dir(request))
#print(request.sid)
#print("TODO")
2019-12-14 19:07:29 +01:00
switch = {
2020-02-21 15:45:30 +01:00
"list": handle_list,
"new": handle_new,
"connect": handle_connect
2019-12-14 19:07:29 +01:00
}
2020-02-21 15:45:30 +01:00
return switch.get(data["action"])(data)