Loading...
Loading...
Compare original and translation side by side
@nestjs/websocketsAppGatewayAppGateway@nestjs/websocketsAppGateway@WebSocketGateway({ path: "/socket.io/", cors: true })
export class AppGateway implements OnGatewayConnection, OnGatewayDisconnect/socket.io/HybridAdapter@builder6/core@WebSocketGateway({ path: "/socket.io/", cors: true })
export class AppGateway implements OnGatewayConnection, OnGatewayDisconnect/socket.io/@builder6/coreHybridAdaptercookiesocket.handshake.headersX-Space-IdX-Auth-TokenAuthService.getUserByToken(token){ user, anonymous, system, portal: { tenantId } }anonymoussocket.handshake.headerscookieX-Space-IdX-Auth-TokenAuthService.getUserByToken(token){ user, anonymous, system, portal: { tenantId } }anonymousRoom format: {tenantId}-{roomPart}
Individual: {roomPart}-{userId}房间格式:{tenantId}-{roomPart}
个人房间:{roomPart}-{userId}// Client-side
socket.emit("subscribe", {
roomParts: ["orders", "notifications"],
individual: false // shared room
});
socket.emit("subscribe", {
roomParts: "notification-change",
individual: true // per-user room: "notification-change-{userId}"
});
socket.emit("unsubscribe", {
roomParts: ["orders"]
});// 客户端代码
socket.emit("subscribe", {
roomParts: ["orders", "notifications"],
individual: false // 共享房间
});
socket.emit("subscribe", {
roomParts: "notification-change",
individual: true // 个人房间:"notification-change-{userId}"
});
socket.emit("unsubscribe", {
roomParts: ["orders"]
});| Event | Payload | Description |
|---|---|---|
| | Join rooms |
| | Leave rooms |
| date | System connection keep-alive |
| 事件 | 负载 | 描述 |
|---|---|---|
| | 加入房间 |
| | 离开房间 |
| 日期 | 系统连接保活 |
| Event | Payload | Description |
|---|---|---|
| — | Sent on successful connection |
| UTC date | Response to ping |
| | Metadata change (objects, fields, apps, etc.) |
| | User notification (per-user room) |
| | Record change event |
| 事件 | 负载 | 描述 |
|---|---|---|
| — | 连接成功时发送 |
| UTC日期 | 对ping的响应 |
| | 元数据变更(对象、字段、应用等) |
| | 用户通知(个人房间) |
| | 记录变更事件 |
// Emitted to ALL connected clients (global broadcast)
socket.on("s:metadata:change", ({ type, action, _id, name, objectName }) => {
// type: "apps", "objects", "object_listviews", "object_actions", "object_fields"
// action: "insert", "update", "delete"
// Refresh UI accordingly
});// 广播至所有连接的客户端(全局广播)
socket.on("s:metadata:change", ({ type, action, _id, name, objectName }) => {
// type: "apps", "objects", "object_listviews", "object_actions", "object_fields"
// action: "insert", "update", "delete"
// 据此刷新UI
});// Client subscribes to a specific record
socket.emit("subscribe", {
roomParts: `record:orders:change-${recordId}`,
individual: false
});
// Server emits when record changes
socket.on(`s:record:orders:change-${recordId}`, ({ _id }) => {
// Reload record
});{tenantId}-record:{objectApiName}:change-{recordId}// 客户端订阅特定记录
socket.emit("subscribe", {
roomParts: `record:orders:change-${recordId}`,
individual: false
});
// 记录变更时服务端发送事件
socket.on(`s:record:orders:change-${recordId}`, ({ _id }) => {
// 重新加载记录
});{tenantId}-record:{objectApiName}:change-{recordId}// Client subscribes
socket.emit("subscribe", {
roomParts: "notification-change",
individual: true // creates room: "notification-change-{userId}"
});
// Server pushes notification
socket.on("s:notification-change", ({ message }) => {
// Show notification
});// 客户端订阅
socket.emit("subscribe", {
roomParts: "notification-change",
individual: true // 创建房间:"notification-change-{userId}"
});
// 服务端推送通知
socket.on("s:notification-change", ({ message }) => {
// 显示通知
});| Moleculer Event | WebSocket Action |
|---|---|
| → |
| → |
| → Generic socket emit with optional room |
| → |
| Moleculer event emitted when client subscribes |
| Moleculer 事件 | WebSocket 操作 |
|---|---|
| → |
| → |
| → 带可选房间参数的通用socket事件发送 |
| → |
| 客户端订阅时触发的Moleculer事件 |
// Emit to specific room from a Moleculer service
broker.emit("$broadcast.socket.emit", {
data: {
eventName: "custom-event",
eventParams: { key: "value" },
room: "tenantId-room-name" // optional, omit for global
}
});
// Send notification to specific users
broker.emit("$broadcast.$notification.users", {
data: {
tenantId: "space_id",
users: ["user1", "user2"],
message: "You have a new task"
}
});// 从Moleculer服务向特定房间发送事件
broker.emit("$broadcast.socket.emit", {
data: {
eventName: "custom-event",
eventParams: { key: "value" },
room: "tenantId-room-name" // 可选参数,省略则全局广播
}
});
// 向特定用户发送通知
broker.emit("$broadcast.$notification.users", {
data: {
tenantId: "space_id",
users: ["user1", "user2"],
message: "您有新任务"
}
});