Skip to content

Commit

Permalink
ditched the Globals and used a higher order func instead;also ditched…
Browse files Browse the repository at this point in the history
… the send thread on_open()
  • Loading branch information
Mohamedemad4 committed Oct 3, 2020
1 parent fa6b010 commit d9cb894
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions tiingo/wsclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,6 @@
import json
from tiingo.exceptions import MissingRequiredArgumentError

GLOB_config=None
GLOB_on_msg_cb=None

class genericWebsocketClient:
'''
the methods passed to websocketClient have to be unbounded if we want WebSocketApp to pass everything correctly
see websocket-client/#471
'''
def on_message(ws, message):
GLOB_on_msg_cb(message)
def on_error(ws, error):
print(error)
def on_close(ws):
pass
def on_open(ws):
def run(*args):
print(GLOB_config)
ws.send(json.dumps(GLOB_config))
thread.start_new_thread(run, ())
def __init__(self,config,on_msg_cb):
global GLOB_config
global GLOB_on_msg_cb
GLOB_config=config
GLOB_on_msg_cb=on_msg_cb
return

class TiingoWebsocketClient:
'''
from tiingo import TiingoWebsocketClient
Expand Down Expand Up @@ -97,14 +71,30 @@ def __init__(self,config={},endpoint=None,on_msg_cb=None):
"def cb_fn(msg):"
" print(msg)")

ws_client = genericWebsocketClient(config=self.config,on_msg_cb=self.on_msg_cb)


websocket.enableTrace(True)
websocket.enableTrace(False)

ws = websocket.WebSocketApp("{0}/{1}".format(self._base_url,self.endpoint),
on_message = genericWebsocketClient.on_message,
on_error = genericWebsocketClient.on_error,
on_close = genericWebsocketClient.on_close,
on_open = genericWebsocketClient.on_open)
ws.run_forever()
on_message = self.get_on_msg_cb(),
on_error = self.on_error,
on_close = self.on_close,
on_open = self.get_on_open(self.config))
ws.run_forever()

def get_on_open(self,config):
# the methods passed to websocketClient have to be unbounded if we want WebSocketApp to pass everything correctly
# see websocket-client/#471
def on_open(ws):
ws.send(json.dumps(config))
return on_open

def get_on_msg_cb(self):
def on_msg_cb_local(ws,msg):
self.on_msg_cb(msg)
return
return on_msg_cb_local

def on_error(ws, error):
print(error)

def on_close(ws):
pass

0 comments on commit d9cb894

Please sign in to comment.