首页 \ 问答 \ 当套接字被连接时,socketio是否会创建一个房间?(Does socketio creates a room always when a socket gets coonected?)

当套接字被连接时,socketio是否会创建一个房间?(Does socketio creates a room always when a socket gets coonected?)

我正在创建一个私人聊天应用程序,希望根据他们的兴趣和行为在房间添加特定的套接字,我希望用户随时都可以进入房间。

我的问题是socket.io是否会在新套接字连接或由于我的代码而发生时自动创建一个房间?

var allSockets = []; // Adding all new sockets in an array

console.log(allSockets.length); // checking length of an array after socket connection

app.get('/api/rooms',function(req,res){
  res.send(io.sockets.adapter.rooms); // just a url to see the rooms connected
})

以上是代码示例,下面是两个用户连接时的输出示例

{"/#DtUmoNBKeQSYMcepAAAA":{"sockets":{"/#DtUmoNBKeQSYMcepAAAA":true},"length":1},"/#1FXDSOViOj2R64DjAAAB":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true},"length":1},"57b2ba38426f18d1040a6d5f":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true,"/#DtUmoNBKeQSYMcepAAAA":true},"length":2}}

我可以在上面的结果中看到,两个用户都有自己的房间以及他们两个在一起的新房间。 所以共有三个房间。

有没有办法摆脱socketio创建的默认房间,如果有的话。


I am creating a private chat app want to add specif sockets on rooms based on their interest and behaviours and i want an user to be in a room at any time.

My question here is does socket.io automatically create a room whenever a new socket gets connected or it happens because of my code?

var allSockets = []; // Adding all new sockets in an array

console.log(allSockets.length); // checking length of an array after socket connection

app.get('/api/rooms',function(req,res){
  res.send(io.sockets.adapter.rooms); // just a url to see the rooms connected
})

Above is an example of code and below is an example of output when two users gets connected

{"/#DtUmoNBKeQSYMcepAAAA":{"sockets":{"/#DtUmoNBKeQSYMcepAAAA":true},"length":1},"/#1FXDSOViOj2R64DjAAAB":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true},"length":1},"57b2ba38426f18d1040a6d5f":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true,"/#DtUmoNBKeQSYMcepAAAA":true},"length":2}}

I can see in the above result that two users have their own room as well a new room created by where they both are together. So total of three rooms.

Is there any way to get rid of default rooms socketio creates if any.


原文:https://stackoverflow.com/questions/38968522
更新时间:2023-07-12 10:07

最满意答案

$对shell和make都有意义,而awk。 如果你想要非make语义,你必须加倍以避免make语义。

第二个例子很简单:它只是$1$3 awk语义。

第一个是$$的shell语义。 $$是:

$$
Process ID (PID) of the script itself. [5] The $$ variable often finds use in scripts to
construct "unique" temp file names (see Example 31-6, Example 16-31, 
and Example 15-27). This is usually simpler than invoking mktemp.

抓住快速报价。


$ has meaning to both the shell and make, and awk. If you want non-make semantics, you have to double to avoid the make semantics.

The second example is thus easy: It's just awk-semantics for the $1 and $3.

The first one is shell semantics for $$. $$ is:

$$
Process ID (PID) of the script itself. [5] The $$ variable often finds use in scripts to
construct "unique" temp file names (see Example 31-6, Example 16-31, 
and Example 15-27). This is usually simpler than invoking mktemp.

to grab a quick quote.

相关问答

更多
  • 内部类如果您的类中存在任何类型,那么类文件将被分类为ClassName$InnerClassName , ClassName$InnerClassName匿名内部类ClassName$InnerClassName ,Class(Java Code)的大小不会导致生成多个类。 例如给定这段代码: public class TestInnerOuterClass { class TestInnerChild{ } Serializable annoymousTest = new Se ...
  • 我们可以使用gsubfn删除括号,并通过从第二组数字中减去一个来更改数字部分 library(gsubfn) v1 <- dummy$cuts v1New <- gsubfn('\\[\\s*(\\d+),\\s*(\\d+)[^0-9]+', ~paste0(x, '-', as.numeric(y)-1), as.character(v1)) table(v1New) # 1-250 251-500 501-750 751-999 # 250 ...
  • $对shell和make都有意义,而awk。 如果你想要非make语义,你必须加倍以避免make语义。 第二个例子很简单:它只是$1和$3 awk语义。 第一个是$$的shell语义。 $$是: $$ Process ID (PID) of the script itself. [5] The $$ variable often finds use in scripts to construct "unique" temp file names (see Example 31-6, Example 16-3 ...
  • 使用encodeURIComponent :