mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-09 09:21:46 +00:00
add connection to webserver logic
This commit is contained in:
@@ -25,13 +25,12 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import webbrowser
|
import webbrowser
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import socketio
|
||||||
import threading
|
import threading
|
||||||
import websockets
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
CONNECTION_CHECK_DELAY = 10
|
|
||||||
connected_sockets = set()
|
sio = socketio.AsyncClient(reconnection=True, reconnection_attempts=3, reconnection_delay=2, logger=True)
|
||||||
websocket_server: websockets.WebSocketServer
|
|
||||||
ws_thread_loop = asyncio.new_event_loop()
|
ws_thread_loop = asyncio.new_event_loop()
|
||||||
ws_process = None
|
ws_process = None
|
||||||
|
|
||||||
@@ -67,7 +66,24 @@ class Web(blenderbim.core.tool.Web):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def connect_websocket_server(cls, port):
|
def connect_websocket_server(cls, port):
|
||||||
pass
|
def set_thread_loop(url):
|
||||||
|
asyncio.set_event_loop(ws_thread_loop)
|
||||||
|
asyncio.run(cls.sio_connect(url))
|
||||||
|
|
||||||
|
ws_url = "ws://localhost:" + str(port) + "/blender"
|
||||||
|
wserver_thread = threading.Thread(target=set_thread_loop, args=(ws_url))
|
||||||
|
wserver_thread.daemon = True
|
||||||
|
wserver_thread.start()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def sio_connect(cls, url):
|
||||||
|
await sio.connect(url, transports=["websocket"], namespaces="/blender")
|
||||||
|
|
||||||
|
# TODO: register event handlers to handle incoming messages
|
||||||
|
# sio.on("data", test.message_handler, namespace="/blender")
|
||||||
|
|
||||||
|
# await sio.emit("data", {"from": "client"}, namespace="/blender")
|
||||||
|
await sio.wait()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def kill_websocket_server(cls):
|
def kill_websocket_server(cls):
|
||||||
@@ -95,54 +111,3 @@ class Web(blenderbim.core.tool.Web):
|
|||||||
data = {}
|
data = {}
|
||||||
data["ifc_file"] = bpy.context.scene.BIMProperties.ifc_file
|
data["ifc_file"] = bpy.context.scene.BIMProperties.ifc_file
|
||||||
return json.dumps(data)
|
return json.dumps(data)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def broadcast(cls, message):
|
|
||||||
for sock in connected_sockets:
|
|
||||||
await sock.send(message)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def ws_server_handler(cls, websocket):
|
|
||||||
connected_sockets.add(websocket)
|
|
||||||
intial_data = cls.get_webui_data()
|
|
||||||
await cls.broadcast(intial_data)
|
|
||||||
try:
|
|
||||||
async for message in websocket:
|
|
||||||
# TODO: handle incoming messages
|
|
||||||
continue
|
|
||||||
finally:
|
|
||||||
connected_sockets.remove(websocket)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def run_ws_server(cls):
|
|
||||||
websocket_server = await websockets.serve(cls.ws_server_handler, "localhost", 8765)
|
|
||||||
print("Web-UI WebSocket server started.")
|
|
||||||
|
|
||||||
# Check if a client is connected every 10 seconds
|
|
||||||
async def check_connections():
|
|
||||||
while True:
|
|
||||||
await asyncio.sleep(CONNECTION_CHECK_DELAY)
|
|
||||||
if not connected_sockets:
|
|
||||||
print("No Web-UI connected, shutting down the WebSocket server")
|
|
||||||
websocket_server.close()
|
|
||||||
break
|
|
||||||
|
|
||||||
await check_connections()
|
|
||||||
await websocket_server.wait_closed()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def open_web_ui(cls):
|
|
||||||
def set_thread_loop():
|
|
||||||
asyncio.set_event_loop(ws_thread_loop)
|
|
||||||
asyncio.run(cls.run_ws_server())
|
|
||||||
|
|
||||||
wserver_thread = threading.Thread(target=set_thread_loop)
|
|
||||||
wserver_thread.daemon = True
|
|
||||||
wserver_thread.start()
|
|
||||||
|
|
||||||
def open_browser():
|
|
||||||
webbrowser.open("file://" + os.path.join(bpy.context.scene.BIMProperties.data_dir, "webui", "index.html"))
|
|
||||||
|
|
||||||
# Delay for 2 seconds before openning browser
|
|
||||||
timer = threading.Timer(2.0, open_browser)
|
|
||||||
timer.start()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user