删除room
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
// server.js - 扩展官方示例
|
||||
const { Signaling } = require('unity-render-streaming');
|
||||
|
||||
class ExtendedSignaling extends Signaling {
|
||||
constructor() {
|
||||
super();
|
||||
this.rooms = new Map(); // 房间信息存储
|
||||
this.connections = new Map(); // 连接信息
|
||||
}
|
||||
|
||||
// 创建房间时记录
|
||||
onCreateConnection(connectionId) {
|
||||
super.onCreateConnection(connectionId);
|
||||
this.connections.set(connectionId, {
|
||||
joinedAt: Date.now(),
|
||||
roomId: null
|
||||
});
|
||||
}
|
||||
|
||||
// 加入房间
|
||||
onJoinRoom(connectionId, roomId) {
|
||||
super.onJoinRoom(connectionId, roomId);
|
||||
|
||||
if (!this.rooms.has(roomId)) {
|
||||
this.rooms.set(roomId, {
|
||||
createdAt: Date.now(),
|
||||
members: new Set()
|
||||
});
|
||||
}
|
||||
this.rooms.get(roomId).members.add(connectionId);
|
||||
this.connections.get(connectionId).roomId = roomId;
|
||||
}
|
||||
|
||||
// 离开房间
|
||||
onLeaveRoom(connectionId, roomId) {
|
||||
super.onLeaveRoom(connectionId, roomId);
|
||||
|
||||
if (this.rooms.has(roomId)) {
|
||||
this.rooms.get(roomId).members.delete(connectionId);
|
||||
if (this.rooms.get(roomId).members.size === 0) {
|
||||
this.rooms.delete(roomId); // 空房间自动清理
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 断开连接
|
||||
onDeleteConnection(connectionId) {
|
||||
const connInfo = this.connections.get(connectionId);
|
||||
if (connInfo && connInfo.roomId) {
|
||||
this.onLeaveRoom(connectionId, connInfo.roomId);
|
||||
}
|
||||
this.connections.delete(connectionId);
|
||||
super.onDeleteConnection(connectionId);
|
||||
}
|
||||
|
||||
// ========== 查询 API ==========
|
||||
|
||||
// 获取所有房间
|
||||
getAllRooms() {
|
||||
return Array.from(this.rooms.entries()).map(([roomId, info]) => ({
|
||||
roomId,
|
||||
memberCount: info.members.size,
|
||||
createdAt: info.createdAt,
|
||||
members: Array.from(info.members)
|
||||
}));
|
||||
}
|
||||
|
||||
// 获取指定房间的成员
|
||||
getRoomMembers(roomId) {
|
||||
const room = this.rooms.get(roomId);
|
||||
if (!room) return null;
|
||||
|
||||
return Array.from(room.members).map(connId => ({
|
||||
connectionId: connId,
|
||||
joinedAt: this.connections.get(connId)?.joinedAt
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP API 暴露查询接口
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
const signaling = new ExtendedSignaling();
|
||||
|
||||
// API: 获取所有房间列表
|
||||
app.get('/api/rooms', (req, res) => {
|
||||
res.json({
|
||||
count: signaling.rooms.size,
|
||||
rooms: signaling.getAllRooms()
|
||||
});
|
||||
});
|
||||
|
||||
// API: 获取指定房间详情
|
||||
app.get('/api/rooms/:roomId', (req, res) => {
|
||||
const { roomId } = req.params;
|
||||
const members = signaling.getRoomMembers(roomId);
|
||||
|
||||
if (!members) {
|
||||
return res.status(404).json({ error: 'Room not found' });
|
||||
}
|
||||
|
||||
res.json({
|
||||
roomId,
|
||||
members,
|
||||
memberCount: members.length
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
signaling.start(80); // WebSocket 信令端口
|
||||
Reference in New Issue
Block a user