jam134 commited on
Commit
38f3fd2
·
verified ·
1 Parent(s): 93db331

Update keep_alive.py

Browse files
Files changed (1) hide show
  1. keep_alive.py +11 -1
keep_alive.py CHANGED
@@ -1,12 +1,13 @@
1
  import time
2
  import threading
3
  import websocket
 
4
 
5
  def on_open(ws):
6
  print("Connection opened")
7
  def run(*args):
8
  while True:
9
- time.sleep(30) # 每30秒發送一次心跳
10
  ws.send("ping") # 發送心跳消息
11
  threading.Thread(target=run).start()
12
 
@@ -26,6 +27,15 @@ def keep_alive_websocket(url):
26
  on_message=on_message,
27
  )
28
  ws.run_forever()
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
  print(f"Error: {e}. Reconnecting in 5 seconds...")
31
  time.sleep(5) # 出錯後等待5秒再重試
 
1
  import time
2
  import threading
3
  import websocket
4
+ import os
5
 
6
  def on_open(ws):
7
  print("Connection opened")
8
  def run(*args):
9
  while True:
10
+ time.sleep(30 * 60) # 每30秒發送一次心跳
11
  ws.send("ping") # 發送心跳消息
12
  threading.Thread(target=run).start()
13
 
 
27
  on_message=on_message,
28
  )
29
  ws.run_forever()
30
+
31
+ # 保持連接5分鐘
32
+ print(f"Staying connected for 5 minutes: {url}")
33
+ time.sleep(5 * 60)
34
+ print(f"Disconnecting from: {url}")
35
+
36
+ ws.close() # 手動關閉連接
37
+ time.sleep(30 * 60) # 等待5分鐘再重新連接
38
+
39
  except Exception as e:
40
  print(f"Error: {e}. Reconnecting in 5 seconds...")
41
  time.sleep(5) # 出錯後等待5秒再重試