管理員 UI
Socket.IO 管理員 UI 可用於概覽 Socket.IO 部署的狀態。
原始碼可在此處找到:https://github.com/socketio/socket.io-admin-ui/
連結至已主機的版本:https://admin.socket.io/
目前功能
- 目前已連線的伺服器和用戶端概覽
- 每個 Socket 實體的詳細資料(主動傳輸、交握、房間...)
- 每個房間的詳細資料
- 伺服器發出或接收的每個事件的詳細資料
- 管理員操作(加入、離開、斷線)
如果您有任何回饋/建議,請不要猶豫!
安裝
伺服器端
首先,安裝 @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),我們不會(也永遠不會)儲存任何關於您或您的瀏覽器的資訊(無追蹤、無分析,...)。話雖如此,自行主機檔案完全沒問題。
您應該會看到以下模式
請輸入伺服器的 URL(例如,https://#:3000
或 https://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
是否將管理員 UI 置於唯讀模式(不允許加入、離開或斷線)。
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
在生產模式中,伺服器不會傳送有關 socket 實例和房間的所有詳細資料,從而減少儀器的記憶體使用量。
instrument(io, {
mode: "production"
});
也可以透過 NODE_ENV 環境變數啟用生產模式
NODE_ENV=production node index.js
運作方式
原始碼可在此處找到:https://github.com/socketio/socket.io-admin-ui/
instrument
方法只會
- 建立 命名空間,並在適用的情況下新增驗證 中間件
- 為每個現有命名空間註冊
connection
和disconnect
事件的監聽器,以追蹤 socket 實例 - 註冊計時器,會定期從伺服器傳送統計資料到 UI
- 註冊 UI 傳送的
join
、leave
和_disconnect
命令的處理常式