修复远程访问:监听所有网络接口 (0.0.0.0)

- 修改 app.listen() 绑定到 0.0.0.0 而不是默认的 localhost
- 自动检测并显示本机 IP 地址
- 更新启动脚本使用正确的数据库端口 (5433)
- 现在可以通过局域网访问 http://192.168.0.250:3000
This commit is contained in:
work-1
2026-02-15 15:13:20 +08:00
parent 71f03f4125
commit d75230522e
3 changed files with 117 additions and 3 deletions
+19 -2
View File
@@ -3,6 +3,7 @@ const { Pool } = require('pg');
const cors = require('cors');
const bodyParser = require('body-parser');
const { v4: uuidv4 } = require('uuid');
const os = require('os');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -368,9 +369,25 @@ app.get('/health', async (req, res) => {
}
});
// 获取本机 IP 地址
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return 'localhost';
}
// 启动服务器
app.listen(PORT, () => {
console.log(`🚀 多智能体任务服务已启动: http://localhost:${PORT}`);
app.listen(PORT, '0.0.0.0', () => {
const localIP = getLocalIP();
console.log(`🚀 多智能体任务服务已启动`);
console.log(` 本地访问: http://localhost:${PORT}`);
console.log(` 远程访问: http://${localIP}:${PORT}`);
});
// 优雅关闭
+1 -1
View File
@@ -33,7 +33,7 @@ echo "启动应用服务..."
pkill -f "node.*server-postgres.js" || true
cd "$(dirname "$0")"
DB_HOST=localhost \
DB_PORT=5432 \
DB_PORT=5433 \
DB_PASSWORD=postgres \
nohup node server-postgres.js > server.log 2>&1 &
+97
View File
@@ -0,0 +1,97 @@
#!/bin/bash
API_BASE="http://localhost:3000/api"
echo "=========================================="
echo " 多智能体任务管理系统 - 功能演示"
echo "=========================================="
echo ""
# 1. 注册智能体
echo "📝 步骤 1: 注册智能体..."
AGENT_RESPONSE=$(curl -s -X POST $API_BASE/agents/register \
-H "Content-Type: application/json" \
-d '{
"name": "演示智能体",
"type": "general",
"capabilities": ["数据分析", "任务处理"]
}')
AGENT_ID=$(echo $AGENT_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "✅ 智能体已注册"
echo " ID: $AGENT_ID"
echo ""
# 2. 查看所有智能体
echo "📋 步骤 2: 查看所有智能体..."
curl -s $API_BASE/agents | jq '.[0] | {id, name, type, status}'
echo ""
# 3. 创建任务
echo "📌 步骤 3: 创建任务..."
TASK_RESPONSE=$(curl -s -X POST $API_BASE/tasks \
-H "Content-Type: application/json" \
-d '{
"title": "测试任务:数据分析",
"description": "分析用户行为数据并生成报告",
"priority": "high",
"created_by": "human"
}')
TASK_ID=$(echo $TASK_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "✅ 任务已创建"
echo " ID: $TASK_ID"
echo ""
# 4. 查看待处理任务
echo "📋 步骤 4: 查看待处理任务..."
curl -s "$API_BASE/tasks?status=pending" | jq '.[0] | {id, title, priority, status}'
echo ""
# 5. 智能体领取任务
echo "🤖 步骤 5: 智能体领取任务..."
curl -s -X POST $API_BASE/tasks/$TASK_ID/claim \
-H "Content-Type: application/json" \
-d "{\"agent_id\": \"$AGENT_ID\"}" | jq '.'
echo ""
# 6. 发送心跳(标记为忙碌)
echo "💓 步骤 6: 发送心跳(忙碌状态)..."
curl -s -X POST $API_BASE/agents/$AGENT_ID/heartbeat \
-H "Content-Type: application/json" \
-d '{"status": "busy"}' | jq '.'
echo ""
# 7. 模拟任务执行
echo "⏳ 步骤 7: 模拟任务执行(3秒)..."
sleep 3
echo ""
# 8. 完成任务
echo "✅ 步骤 8: 标记任务完成..."
curl -s -X PUT $API_BASE/tasks/$TASK_ID \
-H "Content-Type: application/json" \
-d '{
"status": "completed",
"result": "数据分析完成,发现3个关键趋势:1) 用户活跃度提升20% 2) 转化率增长15% 3) 留存率稳定在85%"
}' | jq '.'
echo ""
# 9. 查看任务详情
echo "📊 步骤 9: 查看完成的任务详情..."
curl -s $API_BASE/tasks/$TASK_ID | jq '{id, title, status, result, completed_at}'
echo ""
# 10. 查看统计信息
echo "📈 步骤 10: 查看系统统计..."
curl -s $API_BASE/stats | jq '.'
echo ""
echo "=========================================="
echo " 演示完成!"
echo "=========================================="
echo ""
echo "💡 提示:"
echo " - Web 界面: http://localhost:3000"
echo " - 查看所有智能体: curl $API_BASE/agents | jq"
echo " - 查看所有任务: curl $API_BASE/tasks | jq"