import websocket, sys from threading import Thread from json import dumps from time import sleep import os START_AFTER_CONNECT = None SERVICE_NAME = None def create_pulser(ws: websocket.WebSocketApp): def run(ws): builddate = int(os.getenv("BUILDDATE", "0")) while 1: ws.send_text(dumps({"name":SERVICE_NAME, "builddate":builddate})) sleep(15) thread = Thread(target=run, args=[ws], daemon=True) thread.start() def start(): print("substart") class WS: ws: websocket.WebSocketApp def __init__(self, backend, auth): if (START_AFTER_CONNECT is None or SERVICE_NAME is None): print("Setup START_AFTER_CONNECT and SERVICE_NAME") sys.exit(1) else: print(f"Run after setup {SERVICE_NAME}: {START_AFTER_CONNECT}") addr = backend if not "wss://" in addr: addr = addr.replace("https://", "wss://") if not "/ws/services" in addr: addr += "/ws/services" print(f"Connect to {addr}, secret len: {len(auth)}") self.ws = websocket.WebSocketApp(addr, cookie="secretkey="+auth, on_close=self.on_close, on_open=self.on_open) @staticmethod def on_close(ws_self, status_code, msg): print(f"WS Close, code:{status_code}, msg: {msg}") sys.exit(0) @staticmethod def on_open(ws_self): print("Connected") create_pulser(ws_self) if START_AFTER_CONNECT: START_AFTER_CONNECT() def run(self): if self.ws.run_forever(): print("fuck") if __name__ == "__main__": START_AFTER_CONNECT = start SERVICE_NAME = "test" wsc = WS("wss://tf2.pblr-nyk.pro/ws/services", "") wsc.run()