@lzpenguin/server 1.1.9 → 1.1.11
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 +9 -7
- package/index.js +59 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,8 +23,8 @@ const server = new RiffleServer({
|
|
|
23
23
|
// 1. 监听数据推送(init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送)
|
|
24
24
|
server.onData((data) => {
|
|
25
25
|
console.log('World:', data.world); // 世界数据
|
|
26
|
-
console.log('Self:', data.self); // 自己的公开数据(包含 name 字段)
|
|
27
|
-
console.log('Players:', data.players); // 其他玩家的公开数据(包含 name 字段)
|
|
26
|
+
console.log('Self:', data.self); // 自己的公开数据(包含 name / avatar / online 字段)
|
|
27
|
+
console.log('Players:', data.players); // 其他玩家的公开数据(包含 name / avatar / online 字段)
|
|
28
28
|
// 注意:self 和 players[i] 结构相同,都只包含公开数据
|
|
29
29
|
});
|
|
30
30
|
|
|
@@ -51,17 +51,19 @@ server.update({
|
|
|
51
51
|
```json
|
|
52
52
|
{
|
|
53
53
|
"world": { "score": 0, "level": 1 },
|
|
54
|
-
"self": { "name": "Player1", "x": 100, "y": 100 },
|
|
54
|
+
"self": { "name": "Player1", "avatar": "https://...", "online": true, "x": 100, "y": 100 },
|
|
55
55
|
"players": [
|
|
56
|
-
{ "name": "Player2", "x": 10, "y": 20 },
|
|
57
|
-
{ "name": "Player3", "x": 30, "y": 40 }
|
|
56
|
+
{ "name": "Player2", "avatar": "https://...", "online": true, "x": 10, "y": 20 },
|
|
57
|
+
{ "name": "Player3", "avatar": "https://...", "online": false, "x": 30, "y": 40 }
|
|
58
58
|
]
|
|
59
59
|
}
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
**关于 name 字段:**
|
|
62
|
+
**关于 name / avatar / online 字段:**
|
|
63
63
|
- `self.name` 和 `players[i].name` 字段**一定会存在**,包含用户的昵称或用户名
|
|
64
|
-
-
|
|
64
|
+
- `self.avatar` 和 `players[i].avatar` 字段由服务器根据用户头像覆盖
|
|
65
|
+
- `self.online` 和 `players[i].online` 字段存储在玩家数据中,`/api/v1/server/ws` 连接建立时自动为 true,断开时自动为 false
|
|
66
|
+
- 你不需要在 `init()` 或 `update()` 时设置 name / avatar / online 字段,服务器会直接返回
|
|
65
67
|
|
|
66
68
|
## API 说明
|
|
67
69
|
|
package/index.js
CHANGED
|
@@ -83,10 +83,6 @@ export class RiffleServer {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
if (!baseUrl) {
|
|
87
|
-
throw new Error('Base URL not found');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
86
|
// 从全局变量或 URL 参数中读取 post_id 和 token
|
|
91
87
|
let postId;
|
|
92
88
|
let token;
|
|
@@ -97,11 +93,27 @@ export class RiffleServer {
|
|
|
97
93
|
token = window.__RIFFLE_TOKEN__ || new URLSearchParams(location.search).get('token');
|
|
98
94
|
}
|
|
99
95
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (!token) {
|
|
104
|
-
|
|
96
|
+
// 设置降级模式标志
|
|
97
|
+
this.isDisabled = false;
|
|
98
|
+
|
|
99
|
+
if (!baseUrl || !postId || !token) {
|
|
100
|
+
console.warn('[RiffleServer] Configuration not found (base_url, post_id or token missing), running in disabled mode');
|
|
101
|
+
this.isDisabled = true;
|
|
102
|
+
this.url = null;
|
|
103
|
+
this.postId = null;
|
|
104
|
+
this.token = null;
|
|
105
|
+
this.timestamp = timestamp;
|
|
106
|
+
this.autoReconnect = false;
|
|
107
|
+
this.reconnectInterval = reconnectInterval;
|
|
108
|
+
this.ws = null;
|
|
109
|
+
this.isConnected = false;
|
|
110
|
+
this.isConnecting = false;
|
|
111
|
+
this.isInitialized = false;
|
|
112
|
+
this.reconnectTimer = null;
|
|
113
|
+
this.dataCallbacks = [];
|
|
114
|
+
this.pendingInit = null;
|
|
115
|
+
this.currentData = null;
|
|
116
|
+
return;
|
|
105
117
|
}
|
|
106
118
|
|
|
107
119
|
this.url = baseUrl;
|
|
@@ -133,6 +145,12 @@ export class RiffleServer {
|
|
|
133
145
|
* @private
|
|
134
146
|
*/
|
|
135
147
|
async connect() {
|
|
148
|
+
// 如果处于降级模式,不进行连接
|
|
149
|
+
if (this.isDisabled) {
|
|
150
|
+
console.warn('[RiffleServer] Service is disabled, skipping connection');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
136
154
|
if (this.isConnecting || (this.isConnected && this.ws?.readyState === (WebSocketImpl?.OPEN ?? 1))) {
|
|
137
155
|
return;
|
|
138
156
|
}
|
|
@@ -186,6 +204,28 @@ export class RiffleServer {
|
|
|
186
204
|
|
|
187
205
|
// 检查是否是数据消息(包含 world、self、players 字段)
|
|
188
206
|
if (message.world !== undefined && message.self !== undefined && message.players !== undefined) {
|
|
207
|
+
if (message.self && typeof message.self === 'object') {
|
|
208
|
+
if (message.self.online === undefined) {
|
|
209
|
+
message.self.online = false;
|
|
210
|
+
}
|
|
211
|
+
if (message.self.avatar === undefined) {
|
|
212
|
+
message.self.avatar = '';
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (Array.isArray(message.players)) {
|
|
216
|
+
message.players = message.players.map((player) => {
|
|
217
|
+
if (player && typeof player === 'object') {
|
|
218
|
+
if (player.online === undefined) {
|
|
219
|
+
player.online = false;
|
|
220
|
+
}
|
|
221
|
+
if (player.avatar === undefined) {
|
|
222
|
+
player.avatar = '';
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return player;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
189
229
|
// 如果还未初始化,这可能是 init 响应
|
|
190
230
|
if (!this.isInitialized && message.self) {
|
|
191
231
|
this.isInitialized = true;
|
|
@@ -261,6 +301,11 @@ export class RiffleServer {
|
|
|
261
301
|
* @param {Object} updateData - 更新数据
|
|
262
302
|
*/
|
|
263
303
|
sendUpdate(updateData) {
|
|
304
|
+
// 如果处于降级模式,不发送更新
|
|
305
|
+
if (this.isDisabled) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
264
309
|
if (!this.isConnected || this.ws?.readyState !== WebSocketImpl.OPEN) {
|
|
265
310
|
console.warn('[RiffleServer] Not connected, cannot send update');
|
|
266
311
|
return;
|
|
@@ -304,6 +349,11 @@ export class RiffleServer {
|
|
|
304
349
|
* });
|
|
305
350
|
*/
|
|
306
351
|
init(initData = {}) {
|
|
352
|
+
// 如果处于降级模式,不执行初始化
|
|
353
|
+
if (this.isDisabled) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
307
357
|
// 如果还未连接,保存 init 请求,连接建立后自动执行
|
|
308
358
|
if (!this.isConnected || this.ws?.readyState !== WebSocketImpl.OPEN) {
|
|
309
359
|
console.log('[RiffleServer] Not connected yet, will init after connection');
|