Better handling of errors while sending to (disconnected) clients on the websocket

This commit is contained in:
Gina Häußge 2014-12-21 15:47:24 +01:00
parent 52ad02a08a
commit 363b23d6e3
3 changed files with 17 additions and 6 deletions

View file

@ -70,6 +70,8 @@
it has been changed during runtime by external editing, hence no config settings added manually while the server it has been changed during runtime by external editing, hence no config settings added manually while the server
was running should be overwritten anymore. was running should be overwritten anymore.
* Automatically hard-reload the UI if upon reconnecting to the server a new version is detected. * Automatically hard-reload the UI if upon reconnecting to the server a new version is detected.
* Better handling of errors on the websocket - no more logging of the full stack trace to the log, only a warning
message for now.
### Bug Fixes ### Bug Fixes

View file

@ -96,6 +96,10 @@ ClientOpened
ClientClosed ClientClosed
A client has disconnected from the webserver A client has disconnected from the webserver
Payload:
* ``remoteAddress``: the remote address (IP) of the client that disconnected
Printer communication Printer communication
--------------------- ---------------------

View file

@ -34,6 +34,8 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection):
self._eventManager = eventManager self._eventManager = eventManager
self._pluginManager = pluginManager self._pluginManager = pluginManager
self._remoteAddress = None
def _getRemoteAddress(self, info): def _getRemoteAddress(self, info):
forwardedFor = info.headers.get("X-Forwarded-For") forwardedFor = info.headers.get("X-Forwarded-For")
if forwardedFor is not None: if forwardedFor is not None:
@ -41,8 +43,8 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection):
return info.ip return info.ip
def on_open(self, info): def on_open(self, info):
remoteAddress = self._getRemoteAddress(info) self._remoteAddress = self._getRemoteAddress(info)
self._logger.info("New connection from client: %s" % remoteAddress) self._logger.info("New connection from client: %s" % self._remoteAddress)
# connected => update the API key, might be necessary if the client was left open while the server restarted # connected => update the API key, might be necessary if the client was left open while the server restarted
self._emit("connected", {"apikey": octoprint.server.UI_API_KEY, "version": octoprint.server.VERSION, "display_version": octoprint.server.DISPLAY_VERSION}) self._emit("connected", {"apikey": octoprint.server.UI_API_KEY, "version": octoprint.server.VERSION, "display_version": octoprint.server.DISPLAY_VERSION})
@ -52,20 +54,20 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection):
octoprint.timelapse.registerCallback(self) octoprint.timelapse.registerCallback(self)
self._pluginManager.register_client(self) self._pluginManager.register_client(self)
self._eventManager.fire(Events.CLIENT_OPENED, {"remoteAddress": remoteAddress}) self._eventManager.fire(Events.CLIENT_OPENED, {"remoteAddress": self._remoteAddress})
for event in octoprint.events.all_events(): for event in octoprint.events.all_events():
self._eventManager.subscribe(event, self._onEvent) self._eventManager.subscribe(event, self._onEvent)
octoprint.timelapse.notifyCallbacks(octoprint.timelapse.current) octoprint.timelapse.notifyCallbacks(octoprint.timelapse.current)
def on_close(self): def on_close(self):
self._logger.info("Client connection closed") self._logger.info("Client connection closed: %s" % self._remoteAddress)
self._printer.unregisterCallback(self) self._printer.unregisterCallback(self)
self._fileManager.unregister_slicingprogress_callback(self) self._fileManager.unregister_slicingprogress_callback(self)
octoprint.timelapse.unregisterCallback(self) octoprint.timelapse.unregisterCallback(self)
self._pluginManager.unregister_client(self) self._pluginManager.unregister_client(self)
self._eventManager.fire(Events.CLIENT_CLOSED) self._eventManager.fire(Events.CLIENT_CLOSED, {"remoteAddress": self._remoteAddress})
for event in octoprint.events.all_events(): for event in octoprint.events.all_events():
self._eventManager.unsubscribe(event, self._onEvent) self._eventManager.unsubscribe(event, self._onEvent)
@ -137,4 +139,7 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection):
self.sendEvent(event, payload) self.sendEvent(event, payload)
def _emit(self, type, payload): def _emit(self, type, payload):
self.send({type: payload}) try:
self.send({type: payload})
except Exception as e:
self._logger.warn("Could not send message to client %s: %s" % (self._remoteAddress, e.message))