Implement viewer_{dis,}connect events
This commit is contained in:
parent
fb22cc7528
commit
d1df4ff6a2
48
lua/main.tl
48
lua/main.tl
@ -13,6 +13,7 @@ local socket = Socket.new(ENDPOINT)
|
|||||||
|
|
||||||
print("[MAIN] Setup framebuffer")
|
print("[MAIN] Setup framebuffer")
|
||||||
|
|
||||||
|
local prev_term = term.current()
|
||||||
local orig_native = term.native
|
local orig_native = term.native
|
||||||
local buffer = Framebuffer.wrap(orig_native())
|
local buffer = Framebuffer.wrap(orig_native())
|
||||||
term.native = function(): term.Redirect
|
term.native = function(): term.Redirect
|
||||||
@ -100,23 +101,34 @@ while shell_running do
|
|||||||
else
|
else
|
||||||
e = table.pack(os.pullEventRaw())
|
e = table.pack(os.pullEventRaw())
|
||||||
end
|
end
|
||||||
|
|
||||||
for pid = 1, #tasks do
|
if e[1] == "websocket_message" and e[2] == ENDPOINT then
|
||||||
local task = tasks[pid]
|
local payload = json.decode(e[3] as string) as table
|
||||||
if task.filter == nil or task.filter == e[1] or e[1] == "terminate" then
|
if payload["type"] == "push_event" then
|
||||||
local ok, param = coroutine.resume(task.coro, table.unpack(e as {any}))
|
event_queue:push(payload["event"] as table)
|
||||||
if not ok then
|
elseif payload["type"] == "viewer_connect" then
|
||||||
term.redirect(orig_native())
|
socket:signal_viewer_connect(true)
|
||||||
term.clear()
|
elseif payload["type"] == "viewer_disconnect" then
|
||||||
term.setCursorPos(1,1)
|
socket:signal_viewer_connect(false)
|
||||||
print("OMEGABIG OOF")
|
end
|
||||||
print(("pid %d"):format(pid))
|
else
|
||||||
error(param, 0)
|
for pid = 1, #tasks do
|
||||||
else
|
local task = tasks[pid]
|
||||||
task.filter = param as string
|
if task.filter == nil or task.filter == e[1] or e[1] == "terminate" then
|
||||||
end
|
local ok, param = coroutine.resume(task.coro, table.unpack(e as {any}))
|
||||||
if pid == 1 and coroutine.status(task.coro) == "dead" then
|
if not ok then
|
||||||
shell_running = false
|
term.redirect(orig_native())
|
||||||
|
term.clear()
|
||||||
|
term.setCursorPos(1,1)
|
||||||
|
print("OMEGABIG OOF")
|
||||||
|
print(("pid %d"):format(pid))
|
||||||
|
error(param, 0)
|
||||||
|
else
|
||||||
|
task.filter = param as string
|
||||||
|
end
|
||||||
|
if pid == 1 and coroutine.status(task.coro) == "dead" then
|
||||||
|
shell_running = false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -125,6 +137,6 @@ end
|
|||||||
socket:close()
|
socket:close()
|
||||||
|
|
||||||
term.native = orig_native
|
term.native = orig_native
|
||||||
term.redirect(term.native())
|
term.redirect(prev_term)
|
||||||
term.clear()
|
term.clear()
|
||||||
term.setCursorPos(1,1)
|
term.setCursorPos(1,1)
|
@ -20,6 +20,7 @@ local record Socket
|
|||||||
send: function(self: Socket, message: string)
|
send: function(self: Socket, message: string)
|
||||||
reconnect: function(self: Socket)
|
reconnect: function(self: Socket)
|
||||||
close: function(self: Socket)
|
close: function(self: Socket)
|
||||||
|
signal_viewer_connect: function(self: Socket, connected: boolean)
|
||||||
_endpoint: string
|
_endpoint: string
|
||||||
_callback: StateCallback
|
_callback: StateCallback
|
||||||
_ws: http.Websocket
|
_ws: http.Websocket
|
||||||
@ -73,6 +74,12 @@ impl.close = function(self: Socket)
|
|||||||
self._ws.close()
|
self._ws.close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
impl.signal_viewer_connect = function(self: Socket, connected: boolean)
|
||||||
|
if self:is_bad_state() then return end --how?
|
||||||
|
local new_state: State = connected and "viewer_connected" or "ok"
|
||||||
|
self:_set_state(new_state)
|
||||||
|
end
|
||||||
|
|
||||||
local function new(endpoint: string): Socket
|
local function new(endpoint: string): Socket
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
state = "reset",
|
state = "reset",
|
||||||
|
@ -31,6 +31,18 @@ class WSManager:
|
|||||||
self.viewers: dict[UUID, set[WebSocket]] = dict()
|
self.viewers: dict[UUID, set[WebSocket]] = dict()
|
||||||
self.queue: asyncio.Queue[tuple[UUID, any]] = asyncio.Queue()
|
self.queue: asyncio.Queue[tuple[UUID, any]] = asyncio.Queue()
|
||||||
|
|
||||||
|
async def send_connect(self, uuid: UUID):
|
||||||
|
if uuid not in self.computers:
|
||||||
|
return
|
||||||
|
|
||||||
|
await self.computers[uuid].send_json({"type": "viewer_connect"})
|
||||||
|
|
||||||
|
async def send_disconnect(self, uuid: UUID):
|
||||||
|
if uuid not in self.computers:
|
||||||
|
return
|
||||||
|
|
||||||
|
await self.computers[uuid].send_json({"type": "viewer_disconnect"})
|
||||||
|
|
||||||
async def queue_task(self):
|
async def queue_task(self):
|
||||||
print("[WS] queue task started")
|
print("[WS] queue task started")
|
||||||
while True:
|
while True:
|
||||||
@ -53,6 +65,10 @@ class WSManager:
|
|||||||
|
|
||||||
print(f"[WS] Computer {uuid} connected")
|
print(f"[WS] Computer {uuid} connected")
|
||||||
self.computers[uuid] = socket
|
self.computers[uuid] = socket
|
||||||
|
|
||||||
|
if len(self.viewers.get(uuid, [])) > 0:
|
||||||
|
await self.send_connect(uuid)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data = await socket.receive_json()
|
data = await socket.receive_json()
|
||||||
@ -76,6 +92,9 @@ class WSManager:
|
|||||||
if uuid not in self.viewers:
|
if uuid not in self.viewers:
|
||||||
self.viewers[uuid] = set()
|
self.viewers[uuid] = set()
|
||||||
|
|
||||||
|
if len(self.viewers[uuid]) == 0:
|
||||||
|
await self.send_connect(uuid)
|
||||||
|
|
||||||
self.viewers[uuid].add(socket)
|
self.viewers[uuid].add(socket)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@ -83,8 +102,11 @@ class WSManager:
|
|||||||
data = await socket.receive_json()
|
data = await socket.receive_json()
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
break
|
break
|
||||||
|
|
||||||
self.viewers[uuid].remove(socket)
|
self.viewers[uuid].remove(socket)
|
||||||
|
if len(self.viewers[uuid]) == 0:
|
||||||
|
await self.send_disconnect(uuid)
|
||||||
|
|
||||||
print(f"[WS] Browser disconnected for {uuid}")
|
print(f"[WS] Browser disconnected for {uuid}")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user