【发布时间】:2021-08-19 14:33:49
【问题描述】:
我希望能够在第一个用户连接到 socket.io 房间时创建一些 firebase 引用,然后监听这些 firebase 引用的更改,并写入文件。
现在我的方法是拥有一个全局 Map 对象,然后将 firebase 引用和文件名作为对象的一部分分配给数组。然后将对象存储到全局地图。 不幸的是,一旦第一次连接的功能结束,引用就会丢失。我可以通过让另一个用户连接/刷新页面并记录全局地图来看到这一点。
这是我的代码的样子。
io.on('connect', (socket) => {
io.to(socket.id).emit('create-room');
socket.on('join-room', (roomId: string) => {
socket.join(roomId);
try {
console.log(activeWorkers)
if (!activeWorkers.get(roomId)) {
// if not in global map, tell user he is first in room so that we can get additional info
io.to(socket.id).emit('first-in-room');
} else {
console.log(activeWorkers.get(roomId));
}
} catch(err) {
console.log(err);
}
});
socket.on('create-worker', async (roomId: string, additionalInfo: string) => {
// first user sent back additional info;
let firebaseRefObj = new FirebaseRefObj(roomId, additionalInfo); // an object that stores the references and file paths
activeWorkers.set(roomId, {
id: roomId,
worker: firebaseRefObj
}); // store in global map
firebaseRefObj.createRefs(); // create references, the references add initial values to firebase, then watch for changes to update local files.
})
socket.on("disconnect", () => {
socket.removeAllListeners();
});
})
【问题讨论】:
-
忽略题为 Javascript 开发者,他没有意识到 redis 也使用 RAM。我想知道您的脚本是在专用 VM 上运行还是在 Cloud Functions 上运行,因为 Cloud Functions 也可能会超时,并且并非旨在将数据保留太久。
-
我真的不明白任何存储媒体如何处理我想要的东西,我想在哪里拥有活跃的 FIREBASE 侦听器。它将存储在 firebase rtd 中的数据写入磁盘。我目前正在本地机器上进行测试,但计划在我想扩展时将其 dockerize。
标签: node.js firebase socket.io