|
|
@ -32,21 +32,34 @@ class PromptServer():
|
|
|
|
self.web_root = os.path.join(os.path.dirname(
|
|
|
|
self.web_root = os.path.join(os.path.dirname(
|
|
|
|
os.path.realpath(__file__)), "web")
|
|
|
|
os.path.realpath(__file__)), "web")
|
|
|
|
routes = web.RouteTableDef()
|
|
|
|
routes = web.RouteTableDef()
|
|
|
|
|
|
|
|
self.last_node_id = None
|
|
|
|
|
|
|
|
self.client_id = None
|
|
|
|
|
|
|
|
|
|
|
|
@routes.get('/ws')
|
|
|
|
@routes.get('/ws')
|
|
|
|
async def websocket_handler(request):
|
|
|
|
async def websocket_handler(request):
|
|
|
|
ws = web.WebSocketResponse()
|
|
|
|
ws = web.WebSocketResponse()
|
|
|
|
await ws.prepare(request)
|
|
|
|
await ws.prepare(request)
|
|
|
|
|
|
|
|
sid = request.rel_url.query.get('clientId', '')
|
|
|
|
|
|
|
|
if sid:
|
|
|
|
|
|
|
|
# Reusing existing session, remove old
|
|
|
|
|
|
|
|
self.sockets.pop(sid, None)
|
|
|
|
|
|
|
|
else:
|
|
|
|
sid = uuid.uuid4().hex
|
|
|
|
sid = uuid.uuid4().hex
|
|
|
|
|
|
|
|
|
|
|
|
self.sockets[sid] = ws
|
|
|
|
self.sockets[sid] = ws
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# Send initial state to the new client
|
|
|
|
# Send initial state to the new client
|
|
|
|
await self.send("status", { "status": self.get_queue_info(), 'sid': sid }, sid)
|
|
|
|
await self.send("status", { "status": self.get_queue_info(), 'sid': sid }, sid)
|
|
|
|
|
|
|
|
# On reconnect if we are the currently executing client send the current node
|
|
|
|
|
|
|
|
if self.client_id == sid and self.last_node_id is not None:
|
|
|
|
|
|
|
|
await self.send("executing", { "node": self.last_node_id }, sid)
|
|
|
|
|
|
|
|
|
|
|
|
async for msg in ws:
|
|
|
|
async for msg in ws:
|
|
|
|
if msg.type == aiohttp.WSMsgType.ERROR:
|
|
|
|
if msg.type == aiohttp.WSMsgType.ERROR:
|
|
|
|
print('ws connection closed with exception %s' % ws.exception())
|
|
|
|
print('ws connection closed with exception %s' % ws.exception())
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
self.sockets.pop(sid)
|
|
|
|
self.sockets.pop(sid, None)
|
|
|
|
return ws
|
|
|
|
return ws
|
|
|
|
|
|
|
|
|
|
|
|
@routes.get("/")
|
|
|
|
@routes.get("/")
|
|
|
|