lazurit/ml/ws_manager.py

27 lines
698 B
Python

from websocket._exceptions import WebSocketConnectionClosedException
import websocket
class WsManager:
def __init__(self, ws_path, retries_connect=0):
self.ws_path = ws_path
self.retries_connect = retries_connect
self_ws = None
def connect(self):
self.ws = websocket.create_connection(self.ws_path)
self.ws.settimeout(0.1)
def send(self, data):
for i in range(self.retries_connect+1):
if not self.ws:
self.connect()
if not self.ws.connected:
self.ws.close()
self.connect()
self.ws.send(data)
self.ws.recv()