application: remove direct websockets import

Provide a method for websocket handler registration.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2024-01-13 19:47:08 -05:00
parent 69f527b7c7
commit 35785be5dc
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
2 changed files with 19 additions and 8 deletions

View File

@ -33,7 +33,6 @@ from ..common import (
KlippyState
)
from ..utils import json_wrapper as jsonw
from .websockets import WebSocket, BridgeSocket
from streaming_form_data import StreamingFormDataParser, ParseFailedException
from streaming_form_data.targets import FileTarget, ValueTarget, SHA256Target
@ -48,15 +47,17 @@ from typing import (
Dict,
List,
AsyncGenerator,
Type
)
if TYPE_CHECKING:
from tornado.httpserver import HTTPServer
from tornado.websocket import WebSocketHandler
from ..server import Server
from ..eventloop import EventLoop
from ..confighelper import ConfigHelper
from .klippy_connection import KlippyConnection as Klippy
from ..utils import IPAddress
from .websockets import WebsocketManager
from .websockets import WebsocketManager, WebSocket
from .file_manager.file_manager import FileManager
from .announcements import Announcements
from .machine import Machine
@ -196,8 +197,6 @@ class MoonrakerApp:
app_handlers: List[Any] = [
(AnyMatches(), self.mutable_router),
(home_pattern, WelcomeHandler),
(f"{self._route_prefix}/websocket", WebSocket),
(f"{self._route_prefix}/klippysocket", BridgeSocket),
(f"{self._route_prefix}/server/redirect", RedirectHandler),
(f"{self._route_prefix}/server/jsonrpc", RPCHandler)
]
@ -375,6 +374,13 @@ class MoonrakerApp:
f"{self._route_prefix}{pattern}", FileUploadHandler, params
)
def register_websocket_handler(
self, pattern: str, handler: Type[WebSocketHandler]
) -> None:
self.mutable_router.add_handler(
f"{self._route_prefix}{pattern}", handler, None
)
def register_debug_endpoint(
self,
endpoint: str,
@ -459,9 +465,7 @@ class AuthorizedRequestHandler(tornado.web.RequestHandler):
pass
else:
wsm: WebsocketManager = self.server.lookup_component("websockets")
conn = wsm.get_client(conn_id)
if not isinstance(conn, WebSocket):
return None
conn = wsm.get_client_ws(conn_id)
return conn
def write_error(self, status_code: int, **kwargs) -> None:

View File

@ -34,6 +34,7 @@ if TYPE_CHECKING:
from ..server import Server
from .klippy_connection import KlippyConnection as Klippy
from ..confighelper import ConfigHelper
from .application import MoonrakerApp
from .extensions import ExtensionManager
from .authorization import Authorization
from ..utils import IPAddress
@ -50,6 +51,9 @@ class WebsocketManager:
self.clients: Dict[int, BaseRemoteConnection] = {}
self.bridge_connections: Dict[int, BridgeSocket] = {}
self.closed_event: Optional[asyncio.Event] = None
app: MoonrakerApp = self.server.lookup_component("application")
app.register_websocket_handler("/websocket", WebSocket)
app.register_websocket_handler("/klippysocket", BridgeSocket)
self.server.register_endpoint(
"/server/websocket/id", RequestType.GET, self._handle_id_request,
TransportType.WEBSOCKET
@ -130,7 +134,10 @@ class WebsocketManager:
def has_socket(self, ws_id: int) -> bool:
return ws_id in self.clients
def get_client(self, ws_id: int) -> Optional[BaseRemoteConnection]:
def get_client(self, uid: int) -> Optional[BaseRemoteConnection]:
return self.clients.get(uid, None)
def get_client_ws(self, ws_id: int) -> Optional[WebSocket]:
sc = self.clients.get(ws_id, None)
if sc is None or not isinstance(sc, WebSocket):
return None