本文整理匯總了Python中websocket.WebSocketApp方法的典型用法代碼示例。如果您正苦於以下問題:Python websocket.WebSocketApp方法的具體用法?Python websocket.WebSocketApp怎麽用?Python websocket.WebSocketApp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類websocket
的用法示例。
在下文中一共展示了websocket.WebSocketApp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testSockMaskKey
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def testSockMaskKey(self):
""" A WebSocketApp should forward the received mask_key function down
to the actual socket.
"""
def my_mask_key_func():
pass
def on_open(self, *args, **kwargs):
""" Set the value so the test can use it later on and immediately
close the connection.
"""
WebSocketAppTest.get_mask_key_id = id(self.get_mask_key)
self.close()
app = ws.WebSocketApp('ws://echo.websocket.org/', on_open=on_open, get_mask_key=my_mask_key_func)
app.run_forever()
# if numpu is installed, this assertion fail
# Note: We can't use 'is' for comparing the functions directly, need to use 'id'.
# self.assertEqual(WebSocketAppTest.get_mask_key_id, id(my_mask_key_func))
示例2: connect
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def connect(self, apiKey, secretKey, trace=False):
self.host = OKEX_USD_CONTRACT
self.apiKey = apiKey
self.secretKey = secretKey
self.trace = trace
websocket.enableTrace(trace)
self.ws = websocket.WebSocketApp(self.host,
on_message=self.onMessage,
on_error=self.onError,
on_close=self.onClose,
on_open=self.onOpen)
self.thread = Thread(target=self.ws.run_forever, args=(None, None, 60, 30))
self.thread.start()
# ----------------------------------------------------------------------
示例3: run
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def run(self):
token = self.client['config/auth.token']
device_id = self.client['config/app.device_id']
server = self.client['config/auth.server']
server = server.replace('https', "wss") if server.startswith('https') else server.replace('http', "ws")
wsc_url = "%s/embywebsocket?api_key=%s&device_id=%s" % (server, token, device_id)
LOG.info("Websocket url: %s", wsc_url)
self.wsc = websocket.WebSocketApp(wsc_url,
on_message=self.on_message,
on_error=self.on_error)
self.wsc.on_open = self.on_open
while not self.stop:
self.wsc.run_forever(ping_interval=10)
if self.stop:
break
time.sleep(5)
self.client['callback_ws']('WebSocketRestarting')
LOG.info("---<[ websocket ]")
示例4: reconnect
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def reconnect(self):
"""重新連接"""
# 首先關閉之前的連接
#self.close()
# 再執行重連任務
self.ws = websocket.WebSocketApp(self.host,
on_message=self.onMessage,
on_error=self.onError,
on_close=self.onClose,
on_open=self.onOpen)
self.thread = Thread(target=self.ws.run_forever , args = (None , None , 60, 30))
self.thread.start()
#----------------------------------------------------------------------
示例5: _connect
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def _connect(self):
self._log.debug('初始化websocket並發起鏈接')
self._socket = websocket.WebSocketApp(
self._url,
on_open=self._on_open,
on_message=self._on_message,
on_close=self._on_close,
on_error=self._on_error
)
self._socket.run_forever()
# 以下用於重連
while self._reconnect_required.is_set():
if not self._disconnecte_required.is_set():
self._socket.sock = None
delay = self._reconnect_interval
while delay > 0:
self._log.info('%ds 後重連' % delay)
time.sleep(1)
delay -= 1
self._socket.keep_running = True
self._socket.run_forever()
示例6: connect_Subpot
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def connect_Subpot(self, apiKey , secretKey , trace = False):
self.host = GATEIO_SOCKET_URL
self.apiKey = apiKey
self.secretKey = secretKey
self.trace = trace
websocket.enableTrace(trace)
self.ws = websocket.WebSocketApp(self.host,
on_message=self.onMessage,
on_error=self.onError,
on_close=self.onClose,
on_open=self.onOpen)
self.thread = Thread(target = self.ws.run_forever , args = (None , None , 60, 30))
# self.thread_heart = Thread(target = self.run_forever_heart)
self.thread.start()
# self.thread_heart.start()
#----------------------------------------------------------------------
示例7: reconnect
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def reconnect(self):
"""重新連接"""
# 首先關閉之前的連接
#self.close()
# 再執行重連任務
self.ws = websocket.WebSocketApp(self.host,
on_message=self.onMessage,
on_error=self.onError,
on_close=self.onClose,
on_open=self.onOpen)
self.thread = Thread(target=self.ws.run_forever , args = (None , None , 60, 30))
self.thread.start()
#----------------------------------------------------------------------
示例8: connect_Subpot
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def connect_Subpot(self , trace = False):
self.host = FCOIN_WSS_HOST
self.trace = trace
websocket.enableTrace(trace)
self.ws = websocket.WebSocketApp(self.host,
on_message=self.onMessage,
on_error=self.onError,
on_close=self.onClose,
on_open=self.onOpen)
self.thread = Thread(target = self.ws.run_forever , args = (None , None , 60, 30))
# self.thread_heart = Thread(target = self.run_forever_heart)
self.thread.start()
# self.thread_heart.start()
#----------------------------------------------------------------------
示例9: api_connect
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def api_connect(self, url):
# Proxy support adapted from the UStreamTV plugin (ustreamtv.py)
proxy_url = self.session.get_option("https-proxy")
if proxy_url is None:
proxy_url = self.session.get_option("http-proxy")
proxy_options = parse_proxy_url(proxy_url)
if proxy_options.get('http_proxy_host'):
_log.debug("Using proxy ({0}://{1}:{2})".format(
proxy_options.get('proxy_type') or "http",
proxy_options.get('http_proxy_host'),
proxy_options.get('http_proxy_port') or 80))
_log.debug("Connecting: {0}".format(url))
self._ws = websocket.WebSocketApp(
url,
header=["User-Agent: {0}".format(useragents.CHROME)],
on_open=self.api_on_open,
on_message=self.handle_api_message,
on_error=self.api_on_error)
self.ws_worker_thread = threading.Thread(
target=self._ws.run_forever,
args=proxy_options)
self.ws_worker_thread.daemon = True
self.ws_worker_thread.start()
示例10: run_subscription_batch_jobs
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def run_subscription_batch_jobs(self):
"""
請求 KLine 實時數據
"""
websocket.enableTrace(False)
self.__ws = websocket.WebSocketApp(
self.HUOBIPRO_WEBSOCKET_URL,
on_message=self.on_message,
on_open=self.on_open,
on_error=self.on_error,
on_close=self.on_close
)
self.__locked = True
# 如果意外退出,等待10秒重新運行
while (True):
self.__ws.run_forever()
QA_util_log_expection("FTW! it quit! Retry 10 seconds later...")
time.sleep(10)
示例11: run
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def run(self):
self.topic_type = wait_topic_ready(self.topic_name, self.url)
#print str(self.topic_type)+" self.topic_type"
if not self.topic_type:
rospy.logerr('Type of topic %s are not equal in the remote and local sides', self.topic_name)
return
topic_type_module, topic_type_name = tuple(self.topic_type.split('/'))
try:
roslib.load_manifest(topic_type_module)
msg_module = import_module(topic_type_module + '.msg')
self.rostype = getattr(msg_module, topic_type_name)
if self.test:
self.publisher = rospy.Publisher(self.topic_name + '_rb', self.rostype, queue_size = self.queue_size)
else:
self.publisher = rospy.Publisher(self.topic_name, self.rostype, queue_size = self.queue_size)
self.ws = websocket.WebSocketApp(self.url, on_message = self.on_message, on_error = self.on_error, on_close = self.on_close, on_open = self.on_open)
rospy.loginfo('Create connection to Rosbridge server %s for subscribed topic %s successfully', self.url, self.topic_name)
self.ws.run_forever()
except ResourceNotFound, e:
rospy.logerr('Proxy for subscribed topic %s init falied. Reason: Could not find the required resource: %s', self.topic_name, str(e))
示例12: _run
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def _run(ws, id):
"""
Takes user input and sends it to a websocket.
:param ws: websocket.WebSocketApp
"""
while True:
x = input("\033[44m Me: ")
print("\033[0m", end="")
data = {}
data['id'] = id
data['text'] = x
json_data = json.dumps(data)
ws.send(json_data)
time.sleep(1)
if x == "[DONE]":
break
ws.close()
示例13: startWebsocket
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def startWebsocket(self):
""" Run the websocket in a thread """
self._tick = {}
iniTick = self.returnTicker()
self._ids = {market: iniTick[market]['id'] for market in iniTick}
for market in iniTick:
self._tick[self._ids[market]] = iniTick[market]
self._ws = WebSocketApp("wss://api2.poloniex.com/",
on_open=self._on_open,
on_message=self._on_message,
on_error=self._on_error,
on_close=self._on_close)
self._t = _Thread(target=self._ws.run_forever)
self._t.daemon = True
self._t._running = True
self._t.start()
logger.info('Websocket thread started')
logger.debug(self._ws.url)
示例14: __init__
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def __init__(self, host: str, port: int, connect_timeout: Optional[float] = None,
password: Optional[str] = None, ws: Optional[websocket.WebSocketApp] = None):
self.host = host
self.port = port
self.connect_timeout = connect_timeout
self.password = password
self.state = self.State.DISCONNECTED
self.ws = ws
self._connected = threading.Event()
self._logged_in = threading.Event()
self._echo_received = threading.Event()
self._response_received = threading.Event()
self._download_chunk_ready = threading.Event()
self._file_transfer_request_ack_received = threading.Event()
self._file_transfer_ack_received = threading.Event()
self._file_transfer_request_successful = True
self._file_transfer_successful = True
self._downloaded_chunks = queue.Queue()
self._password_requested = False
self._running_cmd = None
self._received_echo = None
self._received_response = None
self._paste_header_received = False
self.logger = logging.getLogger(__name__)
示例15: __init__
# 需要導入模塊: import websocket [as 別名]
# 或者: from websocket import WebSocketApp [as 別名]
def __init__(self, test=False):
"""
constructor
"""
self.testnet = test
if test:
domain = 'testnet.bitmex.com'
else:
domain = 'www.bitmex.com'
self.endpoint = 'wss://' + domain + '/realtime?subscribe=tradeBin1m:XBTUSD,' \
'tradeBin5m:XBTUSD,tradeBin1h:XBTUSD,tradeBin1d:XBTUSD,instrument:XBTUSD,' \
'margin,position:XBTUSD,wallet,orderBookL2:XBTUSD'
self.ws = websocket.WebSocketApp(self.endpoint,
on_message=self.__on_message,
on_error=self.__on_error,
on_close=self.__on_close,
header=self.__get_auth())
self.wst = threading.Thread(target=self.__start)
self.wst.daemon = True
self.wst.start()