@wovoon/polaris 0.3.0 → 0.4.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.
package/dist/client.d.ts CHANGED
@@ -45,6 +45,8 @@ export declare class PolarisClient {
45
45
  private readonly baseUrl;
46
46
  private sessionPromise;
47
47
  private target;
48
+ /** 同房号的活动连接登记:重复 room(同房号) 自动离开旧连接(重连安全)。 */
49
+ private readonly activeRooms;
48
50
  constructor(init?: PolarisRequestInit);
49
51
  /** 指定作品目标:deploymentId(运行地址自动识别)或 appId。 */
50
52
  configure(options: {
@@ -65,6 +67,8 @@ export declare class PolarisClient {
65
67
  /**
66
68
  * 房间实时通道(room 能力)。浏览器同源 WebSocket;返回的句柄与平台下发 SDK 行为一致:
67
69
  * send 广播、onMessage 收 {from, data}、onPresence 收 joined/join/leave、onClose 断开。
70
+ * 重连安全:同一客户端对同一房间再次调用 room(roomId) 会自动断开旧连接——
71
+ * 做"重连房间"按钮时直接再调一次 room() 即可,无需(但也不妨)先 leave() 旧的。
68
72
  */
69
73
  room(roomId: string): {
70
74
  send: (data: unknown) => void;
package/dist/client.js CHANGED
@@ -22,6 +22,8 @@ export class PolarisClient {
22
22
  constructor(init = {}) {
23
23
  this.sessionPromise = null;
24
24
  this.target = "";
25
+ /** 同房号的活动连接登记:重复 room(同房号) 自动离开旧连接(重连安全)。 */
26
+ this.activeRooms = new Map();
25
27
  this.baseUrl = (init.baseUrl ?? "").replace(/\/$/, "");
26
28
  }
27
29
  /** 指定作品目标:deploymentId(运行地址自动识别)或 appId。 */
@@ -83,10 +85,13 @@ export class PolarisClient {
83
85
  /**
84
86
  * 房间实时通道(room 能力)。浏览器同源 WebSocket;返回的句柄与平台下发 SDK 行为一致:
85
87
  * send 广播、onMessage 收 {from, data}、onPresence 收 joined/join/leave、onClose 断开。
88
+ * 重连安全:同一客户端对同一房间再次调用 room(roomId) 会自动断开旧连接——
89
+ * 做"重连房间"按钮时直接再调一次 room() 即可,无需(但也不妨)先 leave() 旧的。
86
90
  */
87
91
  room(roomId) {
88
92
  if (!roomId)
89
93
  throw new PolarisError("room(roomId) 需要房间号", "WOVOON_ARGUMENT");
94
+ this.activeRooms.get(roomId)?.leave();
90
95
  const listeners = {
91
96
  message: [], presence: [], close: [],
92
97
  };
@@ -100,6 +105,24 @@ export class PolarisClient {
100
105
  catch { /* 监听器异常不中断通道 */ }
101
106
  }
102
107
  };
108
+ const handle = {
109
+ send: (data) => {
110
+ if (socket && socket.readyState === 1)
111
+ socket.send(JSON.stringify({ type: "msg", data }));
112
+ },
113
+ leave: () => {
114
+ left = true;
115
+ if (this.activeRooms.get(roomId) === handle)
116
+ this.activeRooms.delete(roomId);
117
+ try {
118
+ socket?.close();
119
+ }
120
+ catch { /* 已关闭 */ }
121
+ },
122
+ onMessage: (callback) => { listeners.message.push(callback); },
123
+ onPresence: (callback) => { listeners.presence.push(callback); },
124
+ onClose: (callback) => { listeners.close.push(callback); },
125
+ };
103
126
  this.session().then((data) => {
104
127
  if (left || typeof WebSocket === "undefined") {
105
128
  if (!left)
@@ -109,6 +132,7 @@ export class PolarisClient {
109
132
  const origin = this.baseUrl || `${location.protocol}//${location.host}`;
110
133
  const scheme = origin.startsWith("https") ? "wss" : "ws";
111
134
  socket = new WebSocket(`${scheme}://${origin.replace(/^https?:\/\//, "")}/api/cloud/rooms/${data.appId}/${encodeURIComponent(roomId)}`);
135
+ socket.onopen = () => { this.activeRooms.set(roomId, handle); };
112
136
  socket.onmessage = (event) => {
113
137
  let frame = null;
114
138
  try {
@@ -126,25 +150,18 @@ export class PolarisClient {
126
150
  else if (frame.type === "error")
127
151
  emit("message", { error: frame.message });
128
152
  };
129
- socket.onclose = () => emit("close", {});
130
- socket.onerror = () => emit("close", {});
153
+ socket.onclose = () => {
154
+ if (this.activeRooms.get(roomId) === handle)
155
+ this.activeRooms.delete(roomId);
156
+ emit("close", {});
157
+ };
158
+ socket.onerror = () => {
159
+ if (this.activeRooms.get(roomId) === handle)
160
+ this.activeRooms.delete(roomId);
161
+ emit("close", {});
162
+ };
131
163
  }).catch((error) => emit("close", { error: error.message }));
132
- return {
133
- send: (data) => {
134
- if (socket && socket.readyState === 1)
135
- socket.send(JSON.stringify({ type: "msg", data }));
136
- },
137
- leave: () => {
138
- left = true;
139
- try {
140
- socket?.close();
141
- }
142
- catch { /* 已关闭 */ }
143
- },
144
- onMessage: (callback) => { listeners.message.push(callback); },
145
- onPresence: (callback) => { listeners.presence.push(callback); },
146
- onClose: (callback) => { listeners.close.push(callback); },
147
- };
164
+ return handle;
148
165
  }
149
166
  collection(name) {
150
167
  if (!name)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wovoon/polaris",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "wovoon Polaris SDK——wovoon 大后台云能力客户端(身份/数据集合/云函数/房间实时),协议 1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",