Skip to content

Commit accec8a

Browse files
authored
Add files via upload
1 parent c867978 commit accec8a

File tree

5 files changed

+3713
-0
lines changed

5 files changed

+3713
-0
lines changed

qtsapp/QTSAppClientFactory.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Created on Mon August 8, 08:09:56 2022
5+
@author: DrJuneMoone
6+
"""
7+
8+
from qtsapp.lib import *
9+
from qtsapp.QTSAppClientProtocol import QTSAppClientProtocol
10+
11+
12+
class QTSAppClientFactory(WebSocketClientFactory, ReconnectingClientFactory):
13+
14+
"""Autobahn WebSocket client factory to implement reconnection and custom callbacks."""
15+
16+
protocol = QTSAppClientProtocol
17+
maxDelay = 5
18+
maxRetries = 10
19+
20+
_last_connection_time = None
21+
22+
def __init__(self, *args, **kwargs):
23+
"""Initialize with default callback method values."""
24+
self.debug = False
25+
self.ws = None
26+
self.on_open = None
27+
self.on_error = None
28+
self.on_close = None
29+
self.on_message = None
30+
self.on_connect = None
31+
self.on_reconnect = None
32+
self.on_noreconnect = None
33+
34+
super(QTSAppClientFactory, self).__init__(*args, **kwargs)
35+
36+
def startedConnecting(self, connector): # noqa
37+
"""On connecting start or reconnection."""
38+
if not self._last_connection_time and self.debug:
39+
log.debug("Start WebSocket connection.")
40+
41+
self._last_connection_time = time.time()
42+
43+
def clientConnectionFailed(self, connector, reason): # noqa
44+
"""On connection failure (When connect request fails)"""
45+
if self.retries > 0:
46+
log.error(
47+
"Retrying connection. Retry attempt count: {}. Next retry in around: {} seconds".format(
48+
self.retries, int(round(self.delay))
49+
)
50+
)
51+
52+
# on reconnect callback
53+
if self.on_reconnect:
54+
self.on_reconnect(self.retries)
55+
56+
# Retry the connection
57+
self.retry(connector)
58+
self.send_noreconnect()
59+
60+
def clientConnectionLost(self, connector, reason): # noqa
61+
"""On connection lost (When ongoing connection got disconnected)."""
62+
if self.retries > 0:
63+
# on reconnect callback
64+
if self.on_reconnect:
65+
self.on_reconnect(self.retries)
66+
67+
# Retry the connection
68+
self.retry(connector)
69+
self.send_noreconnect()
70+
71+
def send_noreconnect(self):
72+
"""Callback `no_reconnect` if max retries are exhausted."""
73+
if self.maxRetries is not None and (self.retries > self.maxRetries):
74+
if self.debug:
75+
log.debug("Maximum retries ({}) exhausted.".format(self.maxRetries))
76+
77+
if self.on_noreconnect:
78+
self.on_noreconnect()

qtsapp/QTSAppClientProtocol.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Created on Mon August 8, 08:09:56 2022
5+
@author: DrJuneMoone
6+
"""
7+
8+
9+
from qtsapp.lib import *
10+
11+
12+
class QTSAppClientProtocol(WebSocketClientProtocol):
13+
KEEPALIVE_INTERVAL = 5
14+
15+
def __init__(self, *args, **kwargs):
16+
"""Initialize protocol with all options passed from factory."""
17+
super(QTSAppClientProtocol, self).__init__(*args, **kwargs)
18+
19+
# Overide method
20+
def onConnect(self, response): # noqa
21+
"""Called when WebSocket server connection was established"""
22+
self.factory.ws = self
23+
24+
if self.factory.on_connect:
25+
self.factory.on_connect(self, response)
26+
27+
# Reset reconnect on successful reconnect
28+
self.factory.resetDelay()
29+
30+
# Overide method
31+
def onOpen(self): # noqa
32+
"""Called when the initial WebSocket opening handshake was completed."""
33+
if self.factory.on_open:
34+
self.factory.on_open(self)
35+
36+
# Overide method
37+
def onMessage(self, payload, is_binary): # noqa
38+
"""Called when text or binary message is received."""
39+
if self.factory.on_message:
40+
self.factory.on_message(self, payload, is_binary)
41+
42+
# Overide method
43+
def onClose(self, was_clean, code, reason): # noqa
44+
"""Called when connection is closed."""
45+
if not was_clean:
46+
if self.factory.on_error:
47+
self.factory.on_error(self, code, reason)
48+
49+
if self.factory.on_close:
50+
self.factory.on_close(self, code, reason)
51+
52+
def onPong(self, response): # noqa
53+
"""Called when pong message is received."""
54+
if self._last_pong_time and self.factory.debug:
55+
log.debug(
56+
"last pong was {} seconds back.".format(
57+
time.time() - self._last_pong_time
58+
)
59+
)
60+
61+
self._last_pong_time = time.time()
62+
63+
if self.factory.debug:
64+
log.debug("pong => {}".format(response))
65+
66+
# """
67+
# Custom helper and exposed methods.
68+
# """
69+
# drop existing connection to avoid ghost connection
70+
# self.dropConnection(abort=True)

0 commit comments

Comments
 (0)