diff --git a/grapheneapi/grapheneclient.py b/grapheneapi/grapheneclient.py index 5bf1f4b2..b7bbfb78 100644 --- a/grapheneapi/grapheneclient.py +++ b/grapheneapi/grapheneclient.py @@ -364,7 +364,7 @@ def __init__(self, config): self.wallet_user, self.wallet_password) - core_asset = self.rpc.get_object("1.3.0")[0] + self.core_asset = self.rpc.get_object("1.3.0")[0] # Connect to Witness Node if "witness_url" in available_features: @@ -446,16 +446,16 @@ def __init__(self, config): {"registered-network-broadcast": config.onRegisterNetworkBroadcast}) - core_asset = self.ws.get_object("1.3.0") + self.core_asset = self.ws.get_object("1.3.0") - if not core_asset : + if not self.core_asset : raise Exception("Neither WS nor RPC propery configured!") - if core_asset["symbol"] == "BTS" : + if self.core_asset["symbol"] == "BTS" : self.prefix = "BTS" - elif core_asset["symbol"] == "MUSE" : + elif self.core_asset["symbol"] == "MUSE" : self.prefix = "MUSE" - elif core_asset["symbol"] == "CORE" : + elif self.core_asset["symbol"] == "CORE" : self.prefix = "GPH" """ Get network configuration diff --git a/grapheneapi/graphenewsrpc.py b/grapheneapi/graphenewsrpc.py index 5aff5c49..5b7f9d0b 100644 --- a/grapheneapi/graphenewsrpc.py +++ b/grapheneapi/graphenewsrpc.py @@ -1,5 +1,7 @@ +import threading from websocket import create_connection import json +import time class RPCError(Exception): @@ -41,6 +43,20 @@ def __init__(self, url, user="", password=""): self.api_id["database"] = self.database(api_id=1) self.api_id["history"] = self.history(api_id=1) self.api_id["network_broadcast"] = self.network_broadcast(api_id=1) + # self.enable_pings() + + def _send_ping(self, interval, event): + while not event.wait(interval): + self.last_ping_tm = time.time() + if self.sock: + self.sock.ping() + + def enable_pings(self): + event = threading.Event() + ping_interval = 30 + thread = threading.Thread(target=self._send_ping, args=(ping_interval, event)) + thread.setDaemon(True) + thread.start() def get_call_id(self): """ Get the ID for the next RPC call """