@lzpenguin/server 1.1.8 → 1.1.10
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 +15 -5
- package/index.js +37 -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); //
|
|
27
|
-
console.log('Players:', data.players); //
|
|
26
|
+
console.log('Self:', data.self); // 自己的公开数据(包含 name 字段)
|
|
27
|
+
console.log('Players:', data.players); // 其他玩家的公开数据(包含 name 字段)
|
|
28
28
|
// 注意:self 和 players[i] 结构相同,都只包含公开数据
|
|
29
29
|
});
|
|
30
30
|
|
|
@@ -32,7 +32,8 @@ server.onData((data) => {
|
|
|
32
32
|
server.init({
|
|
33
33
|
world: { score: 0, level: 1 },
|
|
34
34
|
self: {
|
|
35
|
-
public: {
|
|
35
|
+
public: { x: 100, y: 100 }
|
|
36
|
+
// name 字段不需要设置,服务器返回数据会包含
|
|
36
37
|
}
|
|
37
38
|
});
|
|
38
39
|
|
|
@@ -58,6 +59,10 @@ server.update({
|
|
|
58
59
|
}
|
|
59
60
|
```
|
|
60
61
|
|
|
62
|
+
**关于 name 字段:**
|
|
63
|
+
- `self.name` 和 `players[i].name` 字段**一定会存在**,包含用户的昵称或用户名
|
|
64
|
+
- 你不需要在 `init()` 或 `update()` 时设置 name 字段,服务器会直接返回
|
|
65
|
+
|
|
61
66
|
## API 说明
|
|
62
67
|
|
|
63
68
|
### init(data) - 初始化服务器
|
|
@@ -68,7 +73,8 @@ server.update({
|
|
|
68
73
|
server.init({
|
|
69
74
|
world: { score: 0, level: 1 }, // 可选:世界初始数据
|
|
70
75
|
self: {
|
|
71
|
-
public: {
|
|
76
|
+
public: { x: 100, y: 100 } // 可选:玩家公开数据
|
|
77
|
+
// 注意:name 字段不需要设置,服务器返回的数据会包含该字段
|
|
72
78
|
}
|
|
73
79
|
});
|
|
74
80
|
```
|
|
@@ -125,6 +131,9 @@ const server = new RiffleServer({
|
|
|
125
131
|
|
|
126
132
|
// 监听推送
|
|
127
133
|
server.onData((data) => {
|
|
134
|
+
// data.self.name 和 data.players[i].name 一定会存在
|
|
135
|
+
console.log('我的名字:', data.self.name);
|
|
136
|
+
|
|
128
137
|
const myPosition = { x: data.self.x, y: data.self.y };
|
|
129
138
|
const otherPlayers = data.players || [];
|
|
130
139
|
renderPlayers(myPosition, otherPlayers);
|
|
@@ -137,7 +146,8 @@ server.onData((data) => {
|
|
|
137
146
|
// 初始化
|
|
138
147
|
server.init({
|
|
139
148
|
world: { score: 0, level: 1 },
|
|
140
|
-
self: { public: {
|
|
149
|
+
self: { public: { x: 0, y: 0 } }
|
|
150
|
+
// name 字段不需要设置,返回的数据会包含
|
|
141
151
|
});
|
|
142
152
|
|
|
143
153
|
// 更新玩家位置
|
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
|
}
|
|
@@ -261,6 +279,11 @@ export class RiffleServer {
|
|
|
261
279
|
* @param {Object} updateData - 更新数据
|
|
262
280
|
*/
|
|
263
281
|
sendUpdate(updateData) {
|
|
282
|
+
// 如果处于降级模式,不发送更新
|
|
283
|
+
if (this.isDisabled) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
264
287
|
if (!this.isConnected || this.ws?.readyState !== WebSocketImpl.OPEN) {
|
|
265
288
|
console.warn('[RiffleServer] Not connected, cannot send update');
|
|
266
289
|
return;
|
|
@@ -304,6 +327,11 @@ export class RiffleServer {
|
|
|
304
327
|
* });
|
|
305
328
|
*/
|
|
306
329
|
init(initData = {}) {
|
|
330
|
+
// 如果处于降级模式,不执行初始化
|
|
331
|
+
if (this.isDisabled) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
307
335
|
// 如果还未连接,保存 init 请求,连接建立后自动执行
|
|
308
336
|
if (!this.isConnected || this.ws?.readyState !== WebSocketImpl.OPEN) {
|
|
309
337
|
console.log('[RiffleServer] Not connected yet, will init after connection');
|