comparison libervia/server/websockets.py @ 1506:ce879da7fcf7

server: fix `on_signal` callback
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 17:50:54 +0100
parents 409d10211b20
children 106bae41f5c8
comparison
equal deleted inserted replaced
1505:a169cbc315f0 1506:ce879da7fcf7
39 39
40 def __init__(self): 40 def __init__(self):
41 super().__init__() 41 super().__init__()
42 self._init_ok: bool = False 42 self._init_ok: bool = False
43 self.__profile: Optional[str] = None 43 self.__profile: Optional[str] = None
44 self.__session: Optional[session_iface.SATSession] = None 44 self.__session: Optional[session_iface.WebSession] = None
45 45
46 @property 46 @property
47 def init_ok(self): 47 def init_ok(self):
48 return self._init_ok 48 return self._init_ok
49 49
50 def send(self, data_type: str, data: dict) -> None: 50 def send(self, data_type: str, data: dict) -> None:
51 """Send data to frontend""" 51 """Send data to frontend"""
52 if not self._init_ok and data_type != "error": 52 if not self._init_ok and data_type != "error":
53 raise exceptions.InternalError( 53 raise exceptions.InternalError(
54 "send called when not initialized, this should not happend! Please use " 54 "send called when not initialized, this should not happend! Please use "
55 "SATSession.send which takes care of sending correctly the data to all " 55 "WebSession.send which takes care of sending correctly the data to all "
56 "sessions." 56 "sessions."
57 ) 57 )
58 58
59 data_root = { 59 data_root = {
60 "type": data_type, 60 "type": data_type,
100 raise types.ConnectionDeny( 100 raise types.ConnectionDeny(
101 types.ConnectionDeny.FORBIDDEN, "Invalid session" 101 types.ConnectionDeny.FORBIDDEN, "Invalid session"
102 ) 102 )
103 103
104 session.touch() 104 session.touch()
105 session_data = session.getComponent(session_iface.ISATSession) 105 session_data = session.getComponent(session_iface.IWebSession)
106 if session_data.ws_socket is not None: 106 if session_data.ws_socket is not None:
107 log.warning("Session socket is already set, force closing it") 107 log.warning("Session socket is already set, force closing it")
108 session_data.ws_socket.send( 108 session_data.ws_socket.send(
109 "force_close", {"reason": "duplicate connection detected"} 109 "force_close", {"reason": "duplicate connection detected"}
110 ) 110 )
201 elif self.__session.ws_socket != self: 201 elif self.__session.ws_socket != self:
202 log.error("session socket is not linked to our instance") 202 log.error("session socket is not linked to our instance")
203 else: 203 else:
204 log.debug(f"reseting websocket session for {self.__profile}") 204 log.debug(f"reseting websocket session for {self.__profile}")
205 self.__session.ws_socket = None 205 self.__session.ws_socket = None
206 sessions = session_iface.SATSession.get_profile_sessions(self.__profile) 206 sessions = session_iface.WebSession.get_profile_sessions(self.__profile)
207 log.debug(f"websocket connection for profile {self.__profile} closed") 207 log.debug(f"websocket connection for profile {self.__profile} closed")
208 self.__profile = None 208 self.__profile = None
209 209
210 @classmethod 210 @classmethod
211 def getBaseURL(cls, secure): 211 def getBaseURL(cls, secure):