fix visible bug
This commit is contained in:
@@ -24,7 +24,7 @@ class Device:
|
|||||||
:param on_error_callback: 当遇到错误时的回调。接收一个参数,为错误对象;若遇到服务器或页面发送的消息不合规,传入ServerException对象
|
:param on_error_callback: 当遇到错误时的回调。接收一个参数,为错误对象;若遇到服务器或页面发送的消息不合规,传入ServerException对象
|
||||||
:param base_url: makergen的基础URL,通常为wss://api.makergen.cn
|
:param base_url: makergen的基础URL,通常为wss://api.makergen.cn
|
||||||
"""
|
"""
|
||||||
url = f"{base_url if base_url[-1] == '/' else base_url + "/"}ws/device/{device_id}?key={device_key}"
|
url = f"{base_url if base_url[-1] == '/' else base_url + '/'}ws/device/{device_id}?key={device_key}"
|
||||||
print(url)
|
print(url)
|
||||||
self.ws = websocket.WebSocketApp(
|
self.ws = websocket.WebSocketApp(
|
||||||
url,
|
url,
|
||||||
@@ -36,11 +36,15 @@ class Device:
|
|||||||
self.on_msg_callback = on_msg_callback if on_msg_callback is not None else lambda _, __ : None
|
self.on_msg_callback = on_msg_callback if on_msg_callback is not None else lambda _, __ : None
|
||||||
self.on_error_callback = on_error_callback if on_error_callback is not None else lambda _ : None
|
self.on_error_callback = on_error_callback if on_error_callback is not None else lambda _ : None
|
||||||
self.is_connected = False
|
self.is_connected = False
|
||||||
|
self._connect_event = threading.Event()
|
||||||
|
|
||||||
def _on_open(self, ws):
|
def _on_open(self, ws):
|
||||||
self.is_connected = True
|
self.is_connected = True
|
||||||
|
self._connect_event.set()
|
||||||
|
|
||||||
def _on_close(self, ws, close_status_code, close_msg):
|
def _on_close(self, ws, close_status_code, close_msg):
|
||||||
self.is_connected = False
|
self.is_connected = False
|
||||||
|
self._connect_event.clear()
|
||||||
|
|
||||||
def _on_message(self, ws, message):
|
def _on_message(self, ws, message):
|
||||||
try:
|
try:
|
||||||
@@ -68,15 +72,13 @@ class Device:
|
|||||||
"""
|
"""
|
||||||
在新线程中运行客户端循环(推荐)
|
在新线程中运行客户端循环(推荐)
|
||||||
"""
|
"""
|
||||||
|
self._connect_event.clear()
|
||||||
wst = threading.Thread(target=self.ws.run_forever)
|
wst = threading.Thread(target=self.ws.run_forever)
|
||||||
wst.daemon = True
|
wst.daemon = True
|
||||||
wst.start()
|
wst.start()
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
while not self.is_connected:
|
if not self._connect_event.wait(timeout=timeout):
|
||||||
if time.time() - start_time > timeout:
|
|
||||||
if not self.is_connected:
|
|
||||||
self.on_error_callback(TimeoutError(f"WebSocket connection timed out after {timeout} seconds"))
|
self.on_error_callback(TimeoutError(f"WebSocket connection timed out after {timeout} seconds"))
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user