def __init__(self, key, secret, channels=[], reconnect=True): # Disabbling logging. # logger = logging.getLogger() # logger.disabled = True # API credentials. self.api_credentials = json.loads('{}') self.api_credentials["apiKey"] = key self.api_credentials["apiSecret"] = secret # Socket parameters. self.socket = Socketcluster.socket( "wss://sc-02.coinigy.com/socketcluster/") self.socket.setreconnection(reconnect) self.channels = {} # Composing different channels. for c in channels: self.channels[c] = Channel() self.socket.onchannel(c, self.channels[c].on_channel_message) # Connecting to socket. self.socket.setBasicListener(self.onconnect, self.ondisconnect, self.onConnectError) self.socket.setAuthenticationListener(self.onSetAuthentication, self.onAuthentication)
def __init__(self, key, secret, channels=[], reconnect=True): # Disabbling logging. logger = logging.getLogger() logger.disabled = True # API credentials. self.api_credentials = json.loads('{}') self.api_credentials["apiKey"] = key self.api_credentials["apiSecret"] = secret # Socket parameters. self.socket = Socketcluster.socket("wss://sc-02.coinigy.com/socketcluster/") self.socket.setreconnection(reconnect) self.pub = Publisher(channels) # Populates the channels dictionary with all the different channels instances as values. for c in channels: channel = Subscriber(c) self.pub.register(c, channel) self.socket.onchannel(c, self.onChannelMessage) # Connecting to socket. self.socket.setBasicListener(self.onconnect, self.ondisconnect, self.onConnectError) self.socket.setAuthenticationListener(self.onSetAuthentication, self.onAuthentication)
def init_socket(): global socket socket = Socketcluster.socket("ws://69.64.32.172:3001/socketcluster/") socket.setBasicListener(onconnect, ondisconnect, onConnectError) socket.setdelay(2) socket.setreconnection(True) socket.connect()
def init_socket(): global socket socket = Socketcluster.socket("ws://" + str(ip_server) + ":3001/socketcluster/") socket.setBasicListener(onconnect, ondisconnect, onConnectError) socket.setdelay(2) socket.setreconnection(True) socket.enablelogger(True) socket.connect()
def connect_ws(self, post_connect_callback, channels): """ Connect to a websocket :channels: List of SockChannel instances """ self.post_conn_cb = post_connect_callback self.channels = channels self.sock = Socketcluster.socket(self.wsendpoint) self.sock.setBasicListener(self.on_connect, self.on_connect_close, self.on_connect_error) self.sock.setAuthenticationListener(self.on_set_auth, self.on_auth) self.sock.setreconnection(False) self.sock.connect() self.log.info(f"Started websocket, listening on {self.wsendpoint}")
def connect_ws(self, post_connect_callback, channels, reconnect=False): """ Connect to a websocket :channels: List of SockChannel instances """ self.post_conn_cb = post_connect_callback self.channels = channels self.wsendpoint = self.context["conf"]["endpoints"].get("websocket") # Skip connecting if we don't have any channels to listen to if not channels: return # Create socket, connect, setting callbacks along the way self.sock = Socketcluster.socket(self.wsendpoint) self.sock.setBasicListener(self._on_connect, self._on_connect_close, self._on_connect_error) self.sock.setAuthenticationListener(self._on_set_auth, self._on_auth) self.sock.setreconnection(reconnect) self.sock.connect()
def connect(self, apiKey, apiSecret, wsURL='wss://sc-02.coinigy.com/socketcluster/'): # Connects to the Coinigy API websocket and initializes data stores self.logger.debug("Connecting Websocket") self.logger.info("Conecting to %s" % wsURL) self.apiKey = apiKey self.apiSecret = apiSecret self.bReadyToUse = False # not yet authenticated self.bError = False # no errors yet self.socket = Socketcluster.socket(wsURL) # create socket connection # set basic event listeners self.socket.setBasicListener(self.__onconnect, self.__ondisconnect, self.__onconnecterror) # set authentication listeners self.socket.setAuthenticationListener(self.__onsetauthentication, self.__onauthentication) # initiate connecting sequence in a separate thread self.socketThread = threading.Thread( target=lambda: self.__socketClusterThread()) self.socketThread.daemon = True self.socketThread.start() self.logger.info("Started thread") # wait for authentication conn_timeout = 50 while (not self.bReadyToUse and conn_timeout and not self.bError): sleep(1) conn_timeout -= 1 if (not conn_timeout or self.bError) and not self.bReadyToUse: self.logger.error("Couldn't get connected. Exiting") # self.exit() sys.exit(1)
def main(): dir_path = os.path.dirname(os.path.realpath(__file__)) config = util.get_config(os.path.join(dir_path, 'trade.conf')) db_session = setup_db(**config['database']) api_credentials = { 'apiKey': config['coinigy']['api_key'], 'apiSecret': config['coinigy']['api_secret'], } tasks = list() for symbol, options in config['symbols'].items(): if options['monitor'] == '1': tasks.append(dict(exchange=options['exchange'], symbol=symbol)) tick = Ticker(tasks, db_session, api_credentials) socket = Socketcluster.socket("wss://sc-02.coinigy.com/socketcluster/") socket.setBasicListener(tick.on_connect, tick.on_disconnect, tick.on_connect_error) socket.setAuthenticationListener(tick.on_set_authentication, tick.on_authentication) socket.setreconnection(True) socket.connect()
def messsageack(key, object, ackmessage): logging.info("Got data " + object + " from key " + key) ackmessage("this is error", "this is data") def ack(key, error, object): logging.info("Got ack data " + object + " and error " + error + " and key is " + key) def puback(channel, error, object): if error is '': logging.info("Publish sent successfully to channel " + channel) def suback(channel, error, object): if error is '': logging.info("Subscribed successfully to channel " + channel) if __name__ == "__main__": socket = Socketcluster.socket("ws://localhost:8000/socketcluster/") socket.setBasicListener(onconnect, ondisconnect, onConnectError) socket.setAuthenticationListener(onSetAuthentication, onAuthentication) socket.onack('ping', messsageack) # socket.enablelogging() # socket.on('yell', message) # socket.setreconnection(True) socket.connect() # socket.connect(sslopt={"cert_reqs": ssl.CERT_NONE})
def ondisconnect(socket): logging.info("on disconnect got called") def onConnectError(socket, error): logging.info("On connect error got called") def onSetAuthentication(socket, token): logging.info("Token received " + token) socket.setAuthtoken(token) def onAuthentication(socket, isauthenticated): logging.info("Authenticated is " + str(isauthenticated)) def ack(eventname, error, data): print("token is " + json.dumps(data, sort_keys=True)) your_code_starts_here(socket) socket.emitack("auth", api_credentials, ack) if __name__ == "__main__": socket = Socketcluster.socket("wss://sc-02.coinigy.com/socketcluster/") socket.setBasicListener(onconnect, ondisconnect, onConnectError) socket.setAuthenticationListener(onSetAuthentication, onAuthentication) socket.setreconnection(False) socket.connect()
def onconnect(socket): print('on connect') def ondisconnect(socket): logging.info("on disconnect got called") def onConnectError(socket, error): logging.info("On connect error got called") if __name__ == "__main__": print(threading.activeCount()) socket = Socketcluster.socket("ws://35.229.77.94:8000/socketcluster/") socket.setBasicListener(onconnect, ondisconnect, onConnectError) def threaded_function(args): socket.connect() thread = Thread(target=threaded_function, args=(2, )) thread.start() def predict_rgb_image(img): result = gesture_names[model.predict_classes(img)[0]] print(result) return (result) def predict_rgb_image_vgg(image): image = np.array(image, dtype='float32')