fix: 修改了中心服务器接收心跳包函数;修改客户端变量类型id
This commit is contained in:
		@@ -29,6 +29,7 @@ def init():
 | 
			
		||||
    asyncio.create_task(receive_heartbeat_internal())
 | 
			
		||||
 | 
			
		||||
def clean_env():
 | 
			
		||||
    clear_database()
 | 
			
		||||
    # 关闭游标和连接
 | 
			
		||||
    cursor.close()
 | 
			
		||||
    conn.close()
 | 
			
		||||
@@ -86,14 +87,14 @@ async def delete_node(ip: str) -> None:
 | 
			
		||||
        print(f"Node with IP {ip} not found.")
 | 
			
		||||
 | 
			
		||||
# 接收节点心跳包
 | 
			
		||||
@app.post("/server/heartbeat")
 | 
			
		||||
@app.get("/server/heartbeat")
 | 
			
		||||
async def receive_heartbeat(ip: str):
 | 
			
		||||
        cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip))
 | 
			
		||||
        return {"status": "received"}     
 | 
			
		||||
    
 | 
			
		||||
async def receive_heartbeat_internal():
 | 
			
		||||
    while 1:
 | 
			
		||||
        timeout = 70
 | 
			
		||||
        timeout = 7
 | 
			
		||||
        # 删除超时的节点
 | 
			
		||||
        # cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,))
 | 
			
		||||
        # conn.commit()
 | 
			
		||||
@@ -120,12 +121,12 @@ async def send_nodes_list(count: int) -> list:
 | 
			
		||||
 | 
			
		||||
    return nodes_list
 | 
			
		||||
 | 
			
		||||
@app.get("/server/clear_database")
 | 
			
		||||
async def clear_database() -> None:
 | 
			
		||||
# @app.get("/server/clear_database")
 | 
			
		||||
def clear_database() -> None:
 | 
			
		||||
    cursor.execute("DELETE FROM nodes")
 | 
			
		||||
    conn.commit()
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    import uvicorn  # pylint: disable=e0401
 | 
			
		||||
 | 
			
		||||
    uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)
 | 
			
		||||
    uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user