Skip to content

Commit 1bb1662

Browse files
committed
Fix connection version + add regression test
1 parent add20e0 commit 1bb1662

File tree

4 files changed

+382
-17
lines changed

4 files changed

+382
-17
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "simplyprint-ws-client"
3-
version = "1.0.1rc37"
3+
version = "1.0.1rc38"
44
description = "SimplyPrint Websocket Client"
55
authors = [{ name = "SimplyPrint", email = "[email protected]" }]
66
requires-python = ">=3.9"

simplyprint_ws_client/core/ws_protocol/connection.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,20 @@ def running(self):
192192
"""Whether the connection loop is running."""
193193
return self._loop_task.task is not None and not self._loop_task.done()
194194

195-
async def _close_ws(self):
196-
"""Close WebSocket connection manually, typically used when we are paused or stopped and no reconnections are taking place."""
197-
if self.connected:
198-
await self.ws.close()
199-
self.ws = None
200-
self.logger.debug("Emitting ConnectionLostEvent due to manual close.")
201-
_ = self.event_bus.emit_task(ConnectionLostEvent(self.v))
202-
self.v += 1
203-
self.logger.info("Manually closed connection.")
195+
async def _close_ws(self, code: int = WSCloseCode.OK, message: bytes = b""):
196+
"""
197+
Close WebSocket connection manually, typically used when we are paused or
198+
stopped and no reconnections are taking place.
199+
"""
200+
if not self.connected:
201+
return
202+
await self.ws.close(code=code, message=message)
203+
self.ws = None
204+
self.logger.debug("Emitting ConnectionLostEvent due to manual close.")
205+
self._state = State.NOT_CONNECTED
206+
_ = self.event_bus.emit_task(ConnectionLostEvent(self.v))
207+
self.v += 1
208+
self.logger.info("Manually closed connection.")
204209

205210
async def _loop(self):
206211
"""Connection main loop - only run once per instance."""
@@ -355,16 +360,11 @@ async def _loop(self):
355360

356361
# Reset connection if waiter is done without having received first message.
357362
if not has_first_msg and wait_first_msg_task.done():
358-
await self.ws.close(
363+
await self._close_ws(
359364
code=WSCloseCode.PROTOCOL_ERROR,
360365
message=b"Did not receive first message in time.",
361366
)
362367

363-
raise ConnectionResetError(
364-
f"Did not receive first message in less"
365-
f" than {WsFirstMessageTimeout} seconds."
366-
)
367-
368368
except WsConnectionErrors as e:
369369
# Disconnect / No connection handling
370370
self._state = State.NOT_CONNECTED

0 commit comments

Comments
 (0)