@roomkit/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +75 -0
  2. package/dist/index.d.ts +9 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +12 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/protocol/index.d.ts +5 -0
  7. package/dist/protocol/index.d.ts.map +1 -0
  8. package/dist/protocol/index.js +5 -0
  9. package/dist/protocol/index.js.map +1 -0
  10. package/dist/protocol/message-id.d.ts +53 -0
  11. package/dist/protocol/message-id.d.ts.map +1 -0
  12. package/dist/protocol/message-id.js +72 -0
  13. package/dist/protocol/message-id.js.map +1 -0
  14. package/dist/protocol/message-type.d.ts +173 -0
  15. package/dist/protocol/message-type.d.ts.map +1 -0
  16. package/dist/protocol/message-type.js +246 -0
  17. package/dist/protocol/message-type.js.map +1 -0
  18. package/dist/protocol/messages.d.ts +78 -0
  19. package/dist/protocol/messages.d.ts.map +1 -0
  20. package/dist/protocol/messages.js +2 -0
  21. package/dist/protocol/messages.js.map +1 -0
  22. package/dist/protocol/payloads.d.ts +106 -0
  23. package/dist/protocol/payloads.d.ts.map +1 -0
  24. package/dist/protocol/payloads.js +105 -0
  25. package/dist/protocol/payloads.js.map +1 -0
  26. package/dist/redis/index.d.ts +3 -0
  27. package/dist/redis/index.d.ts.map +1 -0
  28. package/dist/redis/index.js +3 -0
  29. package/dist/redis/index.js.map +1 -0
  30. package/dist/redis/keys.d.ts +36 -0
  31. package/dist/redis/keys.d.ts.map +1 -0
  32. package/dist/redis/keys.js +60 -0
  33. package/dist/redis/keys.js.map +1 -0
  34. package/dist/redis/lua-scripts.d.ts +16 -0
  35. package/dist/redis/lua-scripts.d.ts.map +1 -0
  36. package/dist/redis/lua-scripts.js +38 -0
  37. package/dist/redis/lua-scripts.js.map +1 -0
  38. package/dist/types/auth.d.ts +76 -0
  39. package/dist/types/auth.d.ts.map +1 -0
  40. package/dist/types/auth.js +15 -0
  41. package/dist/types/auth.js.map +1 -0
  42. package/dist/types/index.d.ts +118 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types/index.js +5 -0
  45. package/dist/types/index.js.map +1 -0
  46. package/dist/types/session.d.ts +45 -0
  47. package/dist/types/session.d.ts.map +1 -0
  48. package/dist/types/session.js +2 -0
  49. package/dist/types/session.js.map +1 -0
  50. package/package.json +62 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Redis Key 模式定义
3
+ * 所有Key使用 gw: 前缀
4
+ */
5
+ export const RedisKeys = {
6
+ // 用户会话(用于断线重连)
7
+ userSession: (userId) => `gw:user:${userId}:session`,
8
+ // 会话令牌到用户ID的映射(用于快速查找)
9
+ sessionToken: (token) => `gw:session:token:${token}`,
10
+ // 用户会话(旧版,保留兼容性)
11
+ session: (userId) => `gw:session:${userId}`,
12
+ // 用户当前房间
13
+ userRoom: (userId) => `gw:user:${userId}:room`,
14
+ // Worker注册信息
15
+ worker: (workerId) => `gw:worker:${workerId}`,
16
+ // 所有Worker列表
17
+ workersAll: () => `gw:workers:all`,
18
+ // 按游戏类型的Worker列表
19
+ workersByType: (gameType) => `gw:workers:${gameType}`,
20
+ // Gateway注册信息
21
+ gateway: (gatewayId) => `gw:gateway:${gatewayId}`,
22
+ // 所有Gateway列表
23
+ gatewaysAll: () => `gw:gateways:all`,
24
+ // 房间到Worker的映射
25
+ roomWorker: (roomId) => `gw:room:${roomId}:worker`,
26
+ // 房间成员列表
27
+ roomMembers: (roomId) => `gw:room:${roomId}:members`,
28
+ // 房间状态
29
+ roomState: (roomId) => `gw:room:${roomId}:state`,
30
+ // 所有房间列表
31
+ roomsAll: () => `gw:rooms:all`,
32
+ // 按游戏类型的房间列表
33
+ roomsByType: (gameType) => `gw:rooms:${gameType}`,
34
+ // joinOrCreate 锁(记录正在创建房间的请求数)
35
+ roomCreatingLock: (roomId) => `gw:room:${roomId}:creating`,
36
+ };
37
+ /**
38
+ * Redis Channel 模式定义
39
+ */
40
+ export const RedisChannels = {
41
+ // Gateway -> Worker 请求通道
42
+ rpcWorker: (workerId) => `gw:rpc:worker:${workerId}`,
43
+ // Worker -> Gateway 响应通道
44
+ rpcGateway: (gatewayId) => `gw:rpc:gateway:${gatewayId}`,
45
+ // 广播通道(所有Gateway订阅)
46
+ broadcast: () => `gw:broadcast`,
47
+ // Admin -> Worker 管理通道
48
+ adminWorker: (workerId) => `gw:admin:worker:${workerId}`,
49
+ // 房间创建完成通知通道(用于 joinOrCreate 等待机制)
50
+ roomCreated: (roomId) => `gw:room:${roomId}:created`,
51
+ // 房间迁移请求通道
52
+ migrationRequest: (targetWorkerId) => `gw:migration:request:${targetWorkerId}`,
53
+ // 房间迁移确认通道
54
+ migrationConfirm: (roomId) => `gw:migration:confirm:${roomId}`,
55
+ // 房间接管请求通道(广播)
56
+ takeoverRequest: () => `gw:takeover:request`,
57
+ // 路由更新通道(广播到所有 Gateway)
58
+ routeUpdate: () => `gw:route:update`,
59
+ };
60
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.js","sourceRoot":"","sources":["../../src/redis/keys.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,eAAe;IACf,WAAW,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,WAAW,MAAM,UAAU;IAE5D,uBAAuB;IACvB,YAAY,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,oBAAoB,KAAK,EAAE;IAE5D,iBAAiB;IACjB,OAAO,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,cAAc,MAAM,EAAE;IAEnD,SAAS;IACT,QAAQ,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,WAAW,MAAM,OAAO;IAEtD,aAAa;IACb,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,aAAa,QAAQ,EAAE;IAErD,aAAa;IACb,UAAU,EAAE,GAAG,EAA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AE;IAEtF,WAAW;IACX,gBAAgB,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,wBAAwB,MAAM,EAAE;IAEtE,eAAe;IACf,eAAe,EAAE,GAAG,EAAE,CAAC,qBAAqB;IAE5C,wBAAwB;IACxB,WAAW,EAAE,GAAG,EAAE,CAAC,iBAAiB;CAC5B,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Lua脚本:原子地读取旧session并写入新session
3
+ * 返回旧的gatewayId和connectionId(如果存在)
4
+ */
5
+ export declare const LUA_SET_SESSION = "\nlocal old_gateway = redis.call(\"HGET\", KEYS[1], \"gatewayId\")\nlocal old_conn = redis.call(\"HGET\", KEYS[1], \"connectionId\")\nredis.call(\"HMSET\", KEYS[1], \n \"gatewayId\", ARGV[1],\n \"connectionId\", ARGV[2],\n \"connectedAt\", ARGV[3])\nredis.call(\"EXPIRE\", KEYS[1], 7200)\nif old_gateway then\n return {old_gateway, old_conn}\nend\nreturn nil\n";
6
+ /**
7
+ * Lua脚本:原子地添加房间成员
8
+ * 如果用户已在房间,返回现有信息
9
+ */
10
+ export declare const LUA_ADD_ROOM_MEMBER = "\nlocal existing = redis.call(\"HGET\", KEYS[1], ARGV[1])\nif existing then\n return existing\nend\nredis.call(\"HSET\", KEYS[1], ARGV[1], ARGV[2])\nreturn nil\n";
11
+ /**
12
+ * Lua脚本:原子地移除房间成员并检查是否为空
13
+ * 返回剩余成员数
14
+ */
15
+ export declare const LUA_REMOVE_ROOM_MEMBER = "\nredis.call(\"HDEL\", KEYS[1], ARGV[1])\nreturn redis.call(\"HLEN\", KEYS[1])\n";
16
+ //# sourceMappingURL=lua-scripts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lua-scripts.d.ts","sourceRoot":"","sources":["../../src/redis/lua-scripts.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,eAAe,yXAY3B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,yKAO/B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,qFAGlC,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Lua脚本:原子地读取旧session并写入新session
3
+ * 返回旧的gatewayId和connectionId(如果存在)
4
+ */
5
+ export const LUA_SET_SESSION = `
6
+ local old_gateway = redis.call("HGET", KEYS[1], "gatewayId")
7
+ local old_conn = redis.call("HGET", KEYS[1], "connectionId")
8
+ redis.call("HMSET", KEYS[1],
9
+ "gatewayId", ARGV[1],
10
+ "connectionId", ARGV[2],
11
+ "connectedAt", ARGV[3])
12
+ redis.call("EXPIRE", KEYS[1], 7200)
13
+ if old_gateway then
14
+ return {old_gateway, old_conn}
15
+ end
16
+ return nil
17
+ `;
18
+ /**
19
+ * Lua脚本:原子地添加房间成员
20
+ * 如果用户已在房间,返回现有信息
21
+ */
22
+ export const LUA_ADD_ROOM_MEMBER = `
23
+ local existing = redis.call("HGET", KEYS[1], ARGV[1])
24
+ if existing then
25
+ return existing
26
+ end
27
+ redis.call("HSET", KEYS[1], ARGV[1], ARGV[2])
28
+ return nil
29
+ `;
30
+ /**
31
+ * Lua脚本:原子地移除房间成员并检查是否为空
32
+ * 返回剩余成员数
33
+ */
34
+ export const LUA_REMOVE_ROOM_MEMBER = `
35
+ redis.call("HDEL", KEYS[1], ARGV[1])
36
+ return redis.call("HLEN", KEYS[1])
37
+ `;
38
+ //# sourceMappingURL=lua-scripts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lua-scripts.js","sourceRoot":"","sources":["../../src/redis/lua-scripts.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG;;;;;;;;;;;;CAY9B,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;CAOlC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;CAGrC,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * 认证上下文
3
+ *
4
+ * 提供给 Room.onAuth 方法的上下文信息
5
+ * 参考 Colyseus 设计
6
+ */
7
+ export interface AuthContext {
8
+ /** 客户端传入的认证 token */
9
+ token?: string;
10
+ /** 客户端 IP 地址 */
11
+ ip?: string;
12
+ /** 请求头(部分关键头信息) */
13
+ headers?: Record<string, string>;
14
+ /** 连接 ID */
15
+ connectionId: string;
16
+ /** Gateway ID */
17
+ gatewayId: string;
18
+ /** 用户代理 */
19
+ userAgent?: string;
20
+ /** 时间戳 */
21
+ timestamp: number;
22
+ }
23
+ /**
24
+ * 认证结果
25
+ *
26
+ * Worker AuthService 返回的认证结果
27
+ */
28
+ export interface AuthResult {
29
+ /** 是否验证通过 */
30
+ valid: boolean;
31
+ /** 用户ID(验证成功时) */
32
+ userId?: string;
33
+ /** 错误信息(验证失败时) */
34
+ error?: string;
35
+ /** 额外的用户数据(可选) */
36
+ userData?: Record<string, unknown>;
37
+ }
38
+ /**
39
+ * 房间鉴权结果
40
+ *
41
+ * Room.onAuth 可以返回的类型:
42
+ * - true: 允许加入
43
+ * - RoomAuthData (object): 允许加入,数据附加到 client.auth
44
+ * - false/falsy: 拒绝加入
45
+ * - throw ServerError: 拒绝加入,错误信息会发送给客户端
46
+ *
47
+ * ⚠️ 重要:Room.onAuth 不能设置或修改 userId!
48
+ * userId 必须在连接层(gw:auth)确定,这确保了:
49
+ * 1. 单连接单用户身份
50
+ * 2. Session 正确性
51
+ * 3. 多房间场景下的一致性
52
+ *
53
+ * 返回对象时,会附加到 client.auth(排除 userId 字段)
54
+ */
55
+ export type RoomAuthResult = boolean | RoomAuthData | Promise<boolean | RoomAuthData>;
56
+ /**
57
+ * 房间认证数据
58
+ *
59
+ * 当 Room.onAuth 返回对象时的结构
60
+ * 这些数据会附加到 client.auth,供后续使用
61
+ */
62
+ export interface RoomAuthData {
63
+ /** 其他认证数据(会附加到 client.auth) */
64
+ [key: string]: unknown;
65
+ }
66
+ /**
67
+ * 服务端错误
68
+ *
69
+ * 在 onAuth/onJoin 中可以抛出的错误类型
70
+ * 错误信息会发送给客户端
71
+ */
72
+ export declare class ServerError extends Error {
73
+ readonly code: number | string;
74
+ constructor(code: number | string, message: string);
75
+ }
76
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/types/auth.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC1B,qBAAqB;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,gBAAgB;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ,mBAAmB;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC,YAAY;IACZ,YAAY,EAAE,MAAM,CAAC;IAErB,iBAAiB;IACjB,SAAS,EAAE,MAAM,CAAC;IAElB,WAAW;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,UAAU;IACV,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,aAAa;IACb,KAAK,EAAE,OAAO,CAAC;IAEf,kBAAkB;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,kBAAkB;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,kBAAkB;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,YAAY,GAAG,OAAO,CAAC,OAAO,GAAG,YAAY,CAAC,CAAC;AAEtF;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,+BAA+B;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,KAAK;aAElB,IAAI,EAAE,MAAM,GAAG,MAAM;gBAArB,IAAI,EAAE,MAAM,GAAG,MAAM,EACrC,OAAO,EAAE,MAAM;CAKlB"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 服务端错误
3
+ *
4
+ * 在 onAuth/onJoin 中可以抛出的错误类型
5
+ * 错误信息会发送给客户端
6
+ */
7
+ export class ServerError extends Error {
8
+ code;
9
+ constructor(code, message) {
10
+ super(message);
11
+ this.code = code;
12
+ this.name = 'ServerError';
13
+ }
14
+ }
15
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/types/auth.ts"],"names":[],"mappings":"AA8EA;;;;;GAKG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IAElB;IADlB,YACkB,IAAqB,EACrC,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,SAAI,GAAJ,IAAI,CAAiB;QAIrC,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * 连接状态
3
+ */
4
+ export type ConnectionStatus = 'connected' | 'authenticating' | 'identified' | 'in-room' | 'disconnected';
5
+ export * from './session.js';
6
+ /**
7
+ * Gateway端连接结构
8
+ *
9
+ * 支持单连接多房间:一个用户可以同时在多个房间中
10
+ * 典型场景:用户在大厅(lobby)的同时进入游戏房间
11
+ */
12
+ export interface Connection {
13
+ id: string;
14
+ userId?: string;
15
+ /** @deprecated 使用 rooms 代替 */
16
+ currentRoom?: string;
17
+ /** 当前所在的所有房间 */
18
+ rooms: Set<string>;
19
+ status: ConnectionStatus;
20
+ lastActivity: number;
21
+ createdAt: number;
22
+ /** 认证完成时间(用于空闲超时检查) */
23
+ identifiedAt?: number;
24
+ }
25
+ /**
26
+ * 房间状态
27
+ */
28
+ export type RoomStatus = 'creating' | 'waiting' | 'active' | 'paused' | 'ending' | 'disposed';
29
+ /**
30
+ * 房间成员信息(存储在Redis)
31
+ */
32
+ export interface RoomMember {
33
+ gatewayId: string;
34
+ connectionId: string;
35
+ joinedAt: number;
36
+ }
37
+ /**
38
+ * Worker注册信息
39
+ */
40
+ export interface WorkerInfo {
41
+ workerId: string;
42
+ gameTypes: string[];
43
+ status: 'online' | 'draining' | 'offline';
44
+ roomCount: number;
45
+ playerCount: number;
46
+ lastHeartbeat: number;
47
+ startedAt: number;
48
+ }
49
+ /**
50
+ * 房间信息
51
+ */
52
+ export interface RoomInfo {
53
+ roomId: string;
54
+ gameType: string;
55
+ playerCount: number;
56
+ maxPlayers: number;
57
+ status: RoomStatus;
58
+ workerId: string;
59
+ createdAt: number;
60
+ }
61
+ /**
62
+ * 玩家信息
63
+ */
64
+ export interface PlayerInfo {
65
+ userId: string;
66
+ nickname?: string;
67
+ avatar?: string;
68
+ joinedAt: number;
69
+ }
70
+ /**
71
+ * 房间迁移状态
72
+ */
73
+ export type MigrationStatus = 'pending' | 'snapshotting' | 'transferring' | 'restoring' | 'verifying' | 'completed' | 'failed';
74
+ /**
75
+ * 房间快照 - 用于迁移
76
+ */
77
+ export interface RoomSnapshot {
78
+ roomId: string;
79
+ gameType: string;
80
+ maxPlayers: number;
81
+ config: Record<string, unknown>;
82
+ status: RoomStatus;
83
+ createdAt: number;
84
+ players: Array<{
85
+ userId: string;
86
+ gatewayId: string;
87
+ connectionId: string;
88
+ playerInfo: PlayerInfo;
89
+ }>;
90
+ gameState: unknown;
91
+ snapshotVersion: number;
92
+ snapshotTime: number;
93
+ sourceWorkerId: string;
94
+ }
95
+ /**
96
+ * 房间迁移请求
97
+ */
98
+ export interface RoomMigrationRequest {
99
+ roomId: string;
100
+ sourceWorkerId: string;
101
+ targetWorkerId: string;
102
+ reason?: string;
103
+ priority?: 'low' | 'normal' | 'high';
104
+ }
105
+ /**
106
+ * 房间迁移结果
107
+ */
108
+ export interface RoomMigrationResult {
109
+ success: boolean;
110
+ roomId: string;
111
+ sourceWorkerId: string;
112
+ targetWorkerId: string;
113
+ migrationTime?: number;
114
+ error?: string;
115
+ status: MigrationStatus;
116
+ }
117
+ export * from './auth.js';
118
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,WAAW,GACX,gBAAgB,GAChB,YAAY,GACZ,SAAS,GACT,cAAc,CAAC;AAGnB,cAAc,cAAc,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8BAA8B;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB;IAChB,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACnB,MAAM,EAAE,gBAAgB,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,uBAAuB;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,UAAU,GAClB,UAAU,GACV,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,QAAQ,GACR,UAAU,CAAC;AAEf;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS,CAAC;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,QA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,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,eAAe,CAAC;CACzB;AAGD,cAAc,WAAW,CAAC"}
@@ -0,0 +1,5 @@
1
+ // 会话相关类型
2
+ export * from './session.js';
3
+ // 导出认证相关类型
4
+ export * from './auth.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAUA,SAAS;AACT,cAAc,cAAc,CAAC;AA8I7B,WAAW;AACX,cAAc,WAAW,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 用户会话信息
3
+ *
4
+ * 保存在 Redis 中,用于断线重连
5
+ * 使用 TTL 自动过期(默认5分钟)
6
+ */
7
+ export interface UserSession {
8
+ /** 用户ID */
9
+ userId: string;
10
+ /** 会话令牌(用于重连验证) */
11
+ sessionToken: string;
12
+ /** 当前连接的 Gateway ID */
13
+ gatewayId?: string;
14
+ /** 当前连接的 Connection ID */
15
+ connectionId?: string;
16
+ /** 用户所在的房间信息(包含类型,避免查询 roomState) */
17
+ rooms: Array<{
18
+ roomId: string;
19
+ gameType: string;
20
+ }>;
21
+ /** 最后活动时间 */
22
+ lastActivity: number;
23
+ /** 创建时间 */
24
+ createdAt: number;
25
+ }
26
+ /**
27
+ * 重连请求 Payload
28
+ */
29
+ export interface ReconnectRequest {
30
+ /** 会话令牌 */
31
+ sessionToken: string;
32
+ }
33
+ /**
34
+ * 重连成功响应 Payload
35
+ */
36
+ export interface ReconnectResponse {
37
+ /** 用户ID */
38
+ userId: string;
39
+ /** 恢复的房间列表 */
40
+ rooms: Array<{
41
+ roomId: string;
42
+ gameType: string;
43
+ }>;
44
+ }
45
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/types/session.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC1B,WAAW;IACX,MAAM,EAAE,MAAM,CAAC;IAEf,mBAAmB;IACnB,YAAY,EAAE,MAAM,CAAC;IAErB,uBAAuB;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,0BAA0B;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,qCAAqC;IACrC,KAAK,EAAE,KAAK,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC,CAAC;IAEH,aAAa;IACb,YAAY,EAAE,MAAM,CAAC;IAErB,WAAW;IACX,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,WAAW;IACX,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,WAAW;IACX,MAAM,EAAE,MAAM,CAAC;IAEf,cAAc;IACd,KAAK,EAAE,KAAK,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/types/session.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@roomkit/core",
3
+ "version": "1.0.0",
4
+ "description": "Core types and protocols for Gateway-Worker framework",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./protocol": {
15
+ "types": "./dist/protocol/index.d.ts",
16
+ "import": "./dist/protocol/index.js",
17
+ "default": "./dist/protocol/index.js"
18
+ },
19
+ "./redis": {
20
+ "types": "./dist/redis/index.d.ts",
21
+ "import": "./dist/redis/index.js",
22
+ "default": "./dist/redis/index.js"
23
+ }
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "clean": "rimraf dist"
28
+ },
29
+ "peerDependencies": {
30
+ "ioredis": "^5.0.0"
31
+ },
32
+ "devDependencies": {
33
+ "ioredis": "^5.3.2",
34
+ "typescript": "^5.3.0",
35
+ "rimraf": "^5.0.5"
36
+ },
37
+ "keywords": [
38
+ "gateway",
39
+ "worker",
40
+ "game-server",
41
+ "websocket",
42
+ "multiplayer"
43
+ ],
44
+ "license": "MIT",
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/daxing/gateway-worker.git",
48
+ "directory": "packages/core"
49
+ },
50
+ "bugs": {
51
+ "url": "https://github.com/daxing/gateway-worker/issues"
52
+ },
53
+ "homepage": "https://github.com/daxing/gateway-worker#readme",
54
+ "files": [
55
+ "dist",
56
+ "README.md",
57
+ "LICENSE"
58
+ ],
59
+ "publishConfig": {
60
+ "access": "public"
61
+ }
62
+ }