@wovoon/polaris 0.3.0 → 0.4.1
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/README.md +13 -0
- package/dist/client.d.ts +4 -0
- package/dist/client.js +35 -18
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -26,5 +26,18 @@ room.send({ text: "你好" });
|
|
|
26
26
|
|
|
27
27
|
零构建页面可直接用平台同源下发的全局版:`<script src="/api/cloud/sdk.js"></script>` → `WovoonCloud.*`。
|
|
28
28
|
|
|
29
|
+
## 资源打包最佳实践(大游戏必读)
|
|
30
|
+
|
|
31
|
+
SDK 只负责云能力;**游戏资源的加载速度取决于打包方式**。两条硬规则:
|
|
32
|
+
|
|
33
|
+
- `.wasm`/`.pck`/`.bundle` 等二进制**直接发原始文件**——平台托管有正确 MIME
|
|
34
|
+
(`application/wasm`)、Range 分段与 CDN Brotli。**不要**用导出工具的 base64-JS 分片
|
|
35
|
+
(`xxx.000.js` 串):体积膨胀 33% 且只能串行解码,加载时间成倍增加。
|
|
36
|
+
- 加载用 `Promise.all` 并行 fetch + `WebAssembly.instantiateStreaming` 流式编译;
|
|
37
|
+
首包(引擎+开场)控制在 3MB 内,其余按场景分包按需下载。
|
|
38
|
+
|
|
39
|
+
完整配方(含 Godot/TapTap 导出还原步骤):`docs/recipes/web-asset-optimization.md`。
|
|
40
|
+
`wovoon validate` 与平台发布扫描会对以上反模式给出同文案 WARN。
|
|
41
|
+
|
|
29
42
|
- 协议正典与完整能力说明见仓库 [PROTOCOL.md](https://gitea/wujiwanjie/wovoon-polaris-sdk) 与 [llms.txt](./llms.txt)
|
|
30
43
|
- 本地开发与校验:`npx @wovoon/cli init / dev / validate`
|
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 = () =>
|
|
130
|
-
|
|
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
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "wovoon Polaris SDK——wovoon 大后台云能力客户端(身份/数据集合/云函数/房间实时),协议 1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,4 +34,4 @@
|
|
|
34
34
|
"access": "public",
|
|
35
35
|
"registry": "https://registry.npmjs.org/"
|
|
36
36
|
}
|
|
37
|
-
}
|
|
37
|
+
}
|