zhibo/Log/服务搭建/聊天室-LiveKit服务.md
xiao12feng8 7376df609d 主题:添加聊天室
功能:聊天室功能添加
2026-01-11 14:07:50 +08:00

2.4 KiB
Raw Blame History

LiveKit 语音聊天室服务部署文档

服务信息

项目
服务器IP 1.15.149.240
HTTP端口 7880
TCP端口 7881
UDP端口范围 50000-50100
API Key APIKey123456
API Secret YourSecretKey123456789012345678901234567890
WebSocket地址 wss://1.15.149.240:7880

一、安装步骤

1. 安装Docker如果没有

curl -fsSL https://get.docker.com | sh

2. 创建配置目录

mkdir -p /opt/livekit
cd /opt/livekit

3. 创建配置文件

cat > /opt/livekit/livekit.yaml << 'EOF'
port: 7880
rtc:
  port_range_start: 50000
  port_range_end: 50100
  use_external_ip: true
keys:
  APIKey123456: YourSecretKey123456789012345678901234567890
logging:
  level: info
EOF

4. 启动LiveKit服务器使用host网络模式

docker run -d \
  --name livekit \
  --restart always \
  --network host \
  -v /opt/livekit/livekit.yaml:/livekit.yaml \
  livekit/livekit-server \
  --config /livekit.yaml

5. 检查是否启动成功

docker ps | grep livekit
docker logs livekit

二、防火墙/安全组配置

需要开放以下端口:

协议 端口 用途
TCP 7880 HTTP API / WebSocket
TCP 7881 WebRTC TCP
UDP 50000-50100 WebRTC 媒体传输

三、常用命令

# 查看服务状态
docker ps | grep livekit

# 查看日志
docker logs livekit
docker logs -f livekit  # 实时查看

# 重启服务
docker restart livekit

# 停止服务
docker stop livekit

# 删除服务
docker rm -f livekit

# 更新服务
docker pull livekit/livekit-server
docker rm -f livekit
# 然后重新执行启动命令

四、验证服务

# 本地测试
curl http://localhost:7880

# 外网测试
curl http://1.15.149.240:7880
# 返回 "OK" 表示服务正常

五、后续开发

后端需要实现

  • Token生成接口/api/voice-room/join-token

Android客户端需要

  • 添加依赖:io.livekit:livekit-android:2.0.0
  • 实现语音房间Activity
  • 麦位UI界面

六、注意事项

  1. 生产环境请修改API密钥:当前使用的是示例密钥,正式上线前请更换
  2. 建议配置HTTPS可以用Nginx反向代理添加SSL证书
  3. UDP端口范围当前配置100个端口支持约50人同时连麦