Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MooneDrJune authored Aug 10, 2022
1 parent c867978 commit accec8a
Show file tree
Hide file tree
Showing 5 changed files with 3,713 additions and 0 deletions.
78 changes: 78 additions & 0 deletions qtsapp/QTSAppClientFactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-

"""
Created on Mon August 8, 08:09:56 2022
@author: DrJuneMoone
"""

from qtsapp.lib import *
from qtsapp.QTSAppClientProtocol import QTSAppClientProtocol


class QTSAppClientFactory(WebSocketClientFactory, ReconnectingClientFactory):

"""Autobahn WebSocket client factory to implement reconnection and custom callbacks."""

protocol = QTSAppClientProtocol
maxDelay = 5
maxRetries = 10

_last_connection_time = None

def __init__(self, *args, **kwargs):
"""Initialize with default callback method values."""
self.debug = False
self.ws = None
self.on_open = None
self.on_error = None
self.on_close = None
self.on_message = None
self.on_connect = None
self.on_reconnect = None
self.on_noreconnect = None

super(QTSAppClientFactory, self).__init__(*args, **kwargs)

def startedConnecting(self, connector): # noqa
"""On connecting start or reconnection."""
if not self._last_connection_time and self.debug:
log.debug("Start WebSocket connection.")

self._last_connection_time = time.time()

def clientConnectionFailed(self, connector, reason): # noqa
"""On connection failure (When connect request fails)"""
if self.retries > 0:
log.error(
"Retrying connection. Retry attempt count: {}. Next retry in around: {} seconds".format(
self.retries, int(round(self.delay))
)
)

# on reconnect callback
if self.on_reconnect:
self.on_reconnect(self.retries)

# Retry the connection
self.retry(connector)
self.send_noreconnect()

def clientConnectionLost(self, connector, reason): # noqa
"""On connection lost (When ongoing connection got disconnected)."""
if self.retries > 0:
# on reconnect callback
if self.on_reconnect:
self.on_reconnect(self.retries)

# Retry the connection
self.retry(connector)
self.send_noreconnect()

def send_noreconnect(self):
"""Callback `no_reconnect` if max retries are exhausted."""
if self.maxRetries is not None and (self.retries > self.maxRetries):
if self.debug:
log.debug("Maximum retries ({}) exhausted.".format(self.maxRetries))

if self.on_noreconnect:
self.on_noreconnect()
70 changes: 70 additions & 0 deletions qtsapp/QTSAppClientProtocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-

"""
Created on Mon August 8, 08:09:56 2022
@author: DrJuneMoone
"""


from qtsapp.lib import *


class QTSAppClientProtocol(WebSocketClientProtocol):
KEEPALIVE_INTERVAL = 5

def __init__(self, *args, **kwargs):
"""Initialize protocol with all options passed from factory."""
super(QTSAppClientProtocol, self).__init__(*args, **kwargs)

# Overide method
def onConnect(self, response): # noqa
"""Called when WebSocket server connection was established"""
self.factory.ws = self

if self.factory.on_connect:
self.factory.on_connect(self, response)

# Reset reconnect on successful reconnect
self.factory.resetDelay()

# Overide method
def onOpen(self): # noqa
"""Called when the initial WebSocket opening handshake was completed."""
if self.factory.on_open:
self.factory.on_open(self)

# Overide method
def onMessage(self, payload, is_binary): # noqa
"""Called when text or binary message is received."""
if self.factory.on_message:
self.factory.on_message(self, payload, is_binary)

# Overide method
def onClose(self, was_clean, code, reason): # noqa
"""Called when connection is closed."""
if not was_clean:
if self.factory.on_error:
self.factory.on_error(self, code, reason)

if self.factory.on_close:
self.factory.on_close(self, code, reason)

def onPong(self, response): # noqa
"""Called when pong message is received."""
if self._last_pong_time and self.factory.debug:
log.debug(
"last pong was {} seconds back.".format(
time.time() - self._last_pong_time
)
)

self._last_pong_time = time.time()

if self.factory.debug:
log.debug("pong => {}".format(response))

# """
# Custom helper and exposed methods.
# """
# drop existing connection to avoid ghost connection
# self.dropConnection(abort=True)
Loading

0 comments on commit accec8a

Please sign in to comment.