跳至主要内容
版本: 4.x

管理界面

Socket.IO 管理界面可用于概述 Socket.IO 部署的状态。

源代码可在以下位置找到:https://github.com/socketio/socket.io-admin-ui/

托管版本链接:https://admin.socket.io/

当前功能

  • 当前连接的服务器和客户端概述

Screenshot of the dashboard

  • 每个套接字实例的详细信息(活动传输、握手、房间等)

Screenshot of the page displaying the details of a socket

  • 每个房间的详细信息

Screenshot of the page displaying the details of a room

  • 服务器发出或接收的每个事件的详细信息

Screenshot of the page displaying the list of events

  • 管理操作(加入、离开、断开连接)

如果您有任何反馈/建议,请随时提出!

安装

服务器端

首先,安装 @socket.io/admin-ui

npm i @socket.io/admin-ui

然后在您的 Socket.IO 服务器上调用 instrument 方法

const { createServer } = require("http");
const { Server } = require("socket.io");
const { instrument } = require("@socket.io/admin-ui");

const httpServer = createServer();

const io = new Server(httpServer, {
cors: {
origin: ["https://admin.socket.io"],
credentials: true
}
});

instrument(io, {
auth: false,
mode: "development",
});

httpServer.listen(3000);

该模块与以下内容兼容

  • Socket.IO v4 服务器
  • Socket.IO v3 服务器(>= 3.1.0),但没有对房间的操作(加入、离开、断开连接)

使用 NestJS 的示例

import { instrument } from "@socket.io/admin-ui";

@WebSocketGateway()
export class MyGateway {
// ...
afterInit() {
instrument(this.server, {
auth: false,
mode: "development",
});
}
}

客户端

然后您可以前往 https://admin.socket.io,或托管 ui/dist 文件夹中的文件 此处

重要提示https://admin.socket.io 上的网站完全是静态的(托管在 Vercel 上),我们不会(也永远不会)存储有关您或您的浏览器的任何信息(无跟踪、无分析等)。也就是说,自行托管文件完全没问题。

您应该看到以下模态

login modal screenshot

请输入您的服务器的 URL(例如,http://localhost:3000https://example.com)以及凭据(如果适用)(请参阅 auth 选项 下方)。

可用选项

auth

默认值:-

此选项是必需的。您可以禁用身份验证(请谨慎使用)

instrument(io, {
auth: false
});

或使用基本身份验证

instrument(io, {
auth: {
type: "basic",
username: "admin",
password: "$2b$10$heqvAkYMez.Va6Et2uXInOnkCT6/uQj1brkrbyG3LpopDklcq7ZOS" // "changeit" encrypted with bcrypt
},
});
注意

请注意,bcrypt 包目前不支持以 $2y$ 前缀开头的哈希,该前缀被一些 BCrypt 实现使用(例如 https://bcrypt-generator.com/https://www.bcrypt.fr/)。您可以使用以下方法检查哈希的有效性:

$ node
> require("bcryptjs").compareSync("<the password>", "<the hash>")
true

您可以使用以下方法生成有效的哈希:

$ node
> require("bcryptjs").hashSync("changeit", 10)
'$2b$10$LQUE...'

另请参阅

namespaceName

默认值:/admin

将创建用于处理管理任务的命名空间的名称。

instrument(io, {
namespaceName: "/custom"
});

此命名空间是经典的 Socket.IO 命名空间,您可以使用以下方法访问它:

const adminNamespace = io.of("/admin");

更多信息 此处

readonly

默认值:false

是否将管理界面置于只读模式(不允许加入、离开或断开连接)。

instrument(io, {
readonly: true
});

serverId

默认值:require("os").hostname()

给定服务器的 ID。如果您在同一台机器上有多个 Socket.IO 服务器,则需要为它们提供不同的 ID

instrument(io, {
serverId: `${require("os").hostname()}#${process.pid}`
});

store

默认值:new InMemoryStore()

存储用于存储会话 ID,以便用户在重新连接时不必重新输入凭据。

如果您在多服务器设置中使用基本身份验证,则应提供自定义存储

const { instrument, RedisStore } = require("@socket.io/admin-ui");

instrument(io, {
store: new RedisStore(redisClient)
});

mode

默认值:development

在生产模式下,服务器不会发送有关套接字实例和房间的所有详细信息,从而减少了仪器化的内存占用。

instrument(io, {
mode: "production"
});

生产模式也可以使用 NODE_ENV 环境变量启用

NODE_ENV=production node index.js

工作原理

源代码可在以下位置找到:https://github.com/socketio/socket.io-admin-ui/

instrument 方法只是

  • 创建一个 命名空间,并在适用时添加身份验证 中间件
  • 为每个现有命名空间的 connectiondisconnect 事件注册监听器,以跟踪套接字实例
  • 注册一个计时器,它将定期将服务器的统计信息发送到 UI
  • 注册从 UI 发送的 joinleave_disconnect 命令的处理程序

最新版本