简体   繁体   English

房间如何在socket.io上工作?

[英]How rooms work on socket.io?

I've read socket.io document but its poor. 我已经阅读了socket.io文件,但是它很糟糕。 I have few questions like this: 我有几个这样的问题:

  1. How can i define property for one room like: room.isPlaying , room.createdDate ... 我如何定义一个房间的属性,例如: room.isPlayingroom.createdDate ...
  2. Where the rooms data store? 房间数据存储在哪里? In the memory? 在记忆里? Will memory be auto release after noone left on that room? 没有人离开那个房间后,记忆会自动释放吗? Because if there are too much empty room created will consume server memory 因为如果创建的空房间过多,则会消耗服务器内存
  3. Now, i'm using object to store room data and handle the empty room: delete when no-one left. 现在,我使用对象来存储房间数据并处理空房间:没有人时删除。 Does it the same with using default socket.io room? 使用默认的socket.io房间是否一样?

How can i define property for one room like: room.isPlaying, room.createdDate 我如何定义一个房间的属性,例如:room.isPlaying,room.createdDate

socket.io does not offer you access to a publicly available object for each room. socket.io不允许您访问每个房间的公共可用对象。 You can create your own object for each room and keep them in your own Map object. 您可以为每个房间创建自己的对象,并将其保留在自己的Map对象中。 So, whenever you add someone to a room, you see if you already have a room object for them in your Map and, if not, you create one. 因此,每当您将某人添加到某个房间时,您都会看到自己在地图中是否已有该房间的对象,如果没有,则创建一个对象。 Then you can keep room state on that object. 然后,您可以在该对象上保留房间状态。

Where the rooms data store? 房间数据存储在哪里? In the memory? 在记忆里? Will memory be auto release after noone left on that room? 没有人离开那个房间后,记忆会自动释放吗? Because if there are too much empty room created will consume server memory 因为如果创建的空房间过多,则会消耗服务器内存

socket.io has it's own state information that keeps track of what connections are in a given room. socket.io具有自己的状态信息,可以跟踪给定房间中的连接。 It does not document that state, but you can study the source code or study state in a debugger and find where everything is store. 它没有记录该状态,但是您可以研究源代码或在调试器中研究状态并查找所有内容的存储位置。

For example: 例如:

io.nsps['/'].adapter.rooms

is an object whose properties are a list of all active rooms in the / namespace. 是一个对象,其属性是/名称空间中所有活动房间的列表。 Or, you can use this to get an array of all active rooms: 或者,您可以使用它来获取所有活动房间的数组:

Object.keys(io.nsps['/'].adapter.rooms)

Will memory be auto release after noone left on that room? 没有人离开那个房间后,记忆会自动释放吗? Because if there are too much empty room created will consume server memory 因为如果创建的空房间过多,则会消耗服务器内存

Yes, memory will be released when a room is empty. 是的,如果房间空了,内存将被释放。

Now, i'm using object to store room data and handle the empty room: delete when no-one left. 现在,我使用对象来存储房间数据并处理空房间:没有人时删除。 Does it the same with using default socket.io room? 使用默认的socket.io房间是否一样?

socket.io keeps the data in memory. socket.io将数据保留在内存中。 It will clean up when a room becomes empty. 房间变空时它将清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM