comparison libervia/server/websockets.py @ 1509:106bae41f5c8

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:11 +0200
parents ce879da7fcf7
children ff95501abe74
comparison
equal deleted inserted replaced
1508:ec3ad9abf9f9 1509:106bae41f5c8
112 self.__session = session_data 112 self.__session = session_data
113 self.__profile = session_data.profile or C.SERVICE_PROFILE 113 self.__profile = session_data.profile or C.SERVICE_PROFILE
114 log.debug(f"websocket connection connected for profile {self.__profile}") 114 log.debug(f"websocket connection connected for profile {self.__profile}")
115 return "libervia-page" 115 return "libervia-page"
116 116
117 def onOpen(self): 117 def on_open(self):
118 log.debug("websocket connection opened") 118 log.debug("websocket connection opened")
119 119
120 def onMessage(self, payload: bytes, isBinary: bool) -> None: 120 def onMessage(self, payload: bytes, isBinary: bool) -> None:
121 if self.__session is None: 121 if self.__session is None:
122 raise exceptions.InternalError("empty session, this should never happen") 122 raise exceptions.InternalError("empty session, this should never happen")
188 "session not authorized" 188 "session not authorized"
189 ) 189 )
190 self.sendClose(4401, "Unauthorized") 190 self.sendClose(4401, "Unauthorized")
191 return 191 return
192 192
193 def onClose(self, wasClean, code, reason): 193 def on_close(self, wasClean, code, reason):
194 log.debug(f"closing websocket (profile: {self.__profile}, reason: {reason})") 194 log.debug(f"closing websocket (profile: {self.__profile}, reason: {reason})")
195 if self.__profile is None: 195 if self.__profile is None:
196 log.error("self.__profile should not be None") 196 log.error("self.__profile should not be None")
197 self.__profile = C.SERVICE_PROFILE 197 self.__profile = C.SERVICE_PROFILE
198 198
206 sessions = session_iface.WebSession.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 get_base_url(cls, secure):
212 return "ws{sec}://localhost:{port}".format( 212 return "ws{sec}://localhost:{port}".format(
213 sec="s" if secure else "", 213 sec="s" if secure else "",
214 port=host.options["port_https" if secure else "port"], 214 port=host.options["port_https" if secure else "port"],
215 ) 215 )
216 216
217 @classmethod 217 @classmethod
218 def getResource(cls, secure): 218 def get_resource(cls, secure):
219 factory = websocket.WebSocketServerFactory(cls.getBaseURL(secure)) 219 factory = websocket.WebSocketServerFactory(cls.get_base_url(secure))
220 factory.protocol = cls 220 factory.protocol = cls
221 return resource.WebSocketResource(factory) 221 return resource.WebSocketResource(factory)