Skip to content

Commit

Permalink
Patched websocket transport to support websocket-client 1.0.0 (#61)
Browse files Browse the repository at this point in the history
* Patched websocket transport to support websocket-client 1.0.0

* feat(reconnection, error): minor fixes

* feat(setup): websocket-client version increment

* feat(bash): update-bash

* feat(msgpack): Solve handshake errors

Co-authored-by: mandrewcito <anbaalo@gmail.com>
  • Loading branch information
morpheus65535 and mandrewcito authored May 19, 2021
1 parent 44a1ae6 commit 28e0592
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ before_install:
- pip install -r requirements.txt
- pip install coverage
after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(cat codecov.yml | curl --data-binary @- https://codecov.io/validate)
install:
- cd containers
- docker-compose up -d
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
msgpack==1.0.2
requests>=2.22.0
websocket-client==0.54.0
websocket-client==1.0.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
install_requires=[
"requests>=2.22.0",
"websocket-client==0.54.0",
"websocket-client==1.0.0",
"msgpack==1.0.2"
]
)
3 changes: 3 additions & 0 deletions signalrcore/protocol/messagepack_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def decode_handshake(self, raw_message):
data = json.loads(handshake_data)
return HandshakeResponseMessage(data.get("error", None)), messages
except Exception as ex:
if type(raw_message) is str:
data = json.loads(raw_message[0: raw_message.index("}") + 1])
return HandshakeResponseMessage(data.get("error", None)), []
Helpers.get_logger().error(raw_message)
Helpers.get_logger().error(ex)
raise ex
Expand Down
41 changes: 17 additions & 24 deletions signalrcore/transport/websockets/websocket_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,51 +128,44 @@ def evaluate_handshake(self, message):
self.connection_checker.start()
else:
self.logger.error(msg.error)
self.on_socket_error(msg.error)
self.on_socket_error(self._ws, msg.error)
self.stop()
raise ValueError("Handshake error {0}".format(msg.error))
self.state = ConnectionState.disconnected
return messages

def on_open(self):
def on_open(self, _):
self.logger.debug("-- web socket open --")
msg = self.protocol.handshake_message()
self.send(msg)

def on_close(self):
def on_close(self, callback, close_status_code, close_reason):
self.logger.debug("-- web socket close --")
self.logger.debug(close_status_code)
self.logger.debug(close_reason)
self.state = ConnectionState.disconnected
if self._on_close is not None and callable(self._on_close):
self._on_close()
if callback is not None and callable(callback):
callback()

def on_socket_error(self, error):
def on_socket_error(self, app, error):
"""
Throws error related on
https://github.com/websocket-client/websocket-client/issues/449
Args:
_: Required to support websocket-client version equal or greater than 0.58.0
error ([type]): [description]
Raises:
HubError: [description]
"""
self.logger.debug("-- web socket error --")
if (type(error) is AttributeError and
"'NoneType' object has no attribute 'connected'"
in str(error)):
url = "https://github.com/websocket-client" +\
"/websocket-client/issues/449"
self.logger.warning(
"Websocket closing error: issue" +
url)
self._on_close()
else:
self.logger.error(traceback.format_exc(5, True))
self.logger.error("{0} {1}".format(self, error))
self.logger.error("{0} {1}".format(error, type(error)))
self._on_close()
raise HubError(error)
self.logger.error(traceback.format_exc(10, True))
self.logger.error("{0} {1}".format(self, error))
self.logger.error("{0} {1}".format(error, type(error)))
self._on_close()
self.state = ConnectionState.disconnected
#raise HubError(error)

def on_message(self, raw_message):
def on_message(self, app, raw_message):
self.logger.debug("Message received{0}".format(raw_message))
if not self.handshake_received:
messages = self.evaluate_handshake(raw_message)
Expand Down
11 changes: 1 addition & 10 deletions test/reconnection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,16 @@ def reconnect_test(self, connection):

connection.on_open(lambda: self._lock.release())

connection.on("ReceiveMessage", lambda _: self._lock.release())

connection.start()

self.assertTrue(self._lock.acquire(timeout=10)) # Release on Open

connection.send("DisconnectMe", [])

self.assertTrue(self._lock.acquire(timeout=20)) # released on open

connection.send("SendMessage", ["user", "reconnected!"])

self.assertTrue(self._lock.acquire(timeout=10)) # released at receive message
self.assertTrue(self._lock.acquire(timeout=10)) # released on open

#connection.on_close(lambda: _lock.release())
connection.stop()

#self.assertTrue(_lock.acquire(timeout=10)) # released at close

def test_raw_reconnection(self):
connection = HubConnectionBuilder()\
.with_url(self.server_url, options={"verify_ssl": False})\
Expand Down

0 comments on commit 28e0592

Please sign in to comment.