@lzpenguin/server 1.1.1 → 1.1.2

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 (2) hide show
  1. package/README.md +55 -105
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -13,195 +13,145 @@ npm install @lzpenguin/server
13
13
  ```javascript
14
14
  import { RiffleServer } from '@lzpenguin/server';
15
15
 
16
- // post_id 和 token 会自动从浏览器 URL 参数中读取
17
- // 例如:https://example.com/game.html?post_id=123456&token=your-token-here
16
+ // 创建服务器实例
17
+ // 注意:post_idtoken 会自动从 URL 参数中读取
18
+ // URL 格式:https://example.com/game.html?post_id=123456&token=your-token
18
19
  const server = new RiffleServer({
19
- url: 'wss://api.riffle.app',
20
- timestamp: 'timestamp given in the context' // 游戏时间戳,用于判断是否需要重新初始化
20
+ url: 'wss://api.riffle.app', // 必需:WebSocket 服务器地址
21
+ timestamp: 1234567890, // 必需:游戏时间戳(数字类型),直接使用上下文中给出的值
22
+ autoReconnect: true, // 可选:自动重连,默认 true
23
+ reconnectInterval: 3000 // 可选:重连间隔(毫秒),默认 3000
21
24
  });
22
25
 
23
- // 监听数据更新
26
+ // 1. 监听数据推送(init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送)
24
27
  server.onData((data) => {
25
- console.log('World:', data.world);
26
- console.log('Self:', data.self); // self 现在直接是公开数据,与 players[i] 结构一致
27
- console.log('Players:', data.players);
28
+ console.log('World:', data.world); // 世界数据
29
+ console.log('Self:', data.self); // 自己的公开数据
30
+ console.log('Players:', data.players); // 其他玩家的公开数据
31
+ // 注意:self 和 players[i] 结构相同,都只包含公开数据
28
32
  });
29
33
 
30
- // 初始化服务器
34
+ // 2. 初始化服务器(必需,连接后必须先调用)
31
35
  server.init({
32
36
  world: { score: 0, level: 1 },
33
37
  self: {
34
38
  public: { name: 'Player1', x: 100, y: 100 }
35
39
  }
36
40
  });
37
- ```
38
-
39
- ## API
40
41
 
41
- ### 构造函数
42
-
43
- ```javascript
44
- new RiffleServer({
45
- url: 'wss://api.riffle.app', // 必需:WebSocket 服务器 URL
46
- timestamp: 'timestamp given in the context', // 必需:游戏时间戳(用于判断是否需要重新初始化,新 timestamp 必须大于旧的才会重置)
47
- autoReconnect: true, // 可选:是否自动重连,默认 true
48
- reconnectInterval: 3000 // 可选:重连间隔(毫秒),默认 3000
49
- })
50
- // 注意:post_id 和 token 会自动从浏览器 URL 参数中读取
51
- // 页面 URL 格式:https://example.com/game.html?post_id=123456&token=your-token-here
42
+ // 3. 更新数据(部分更新,合并到现有数据)
43
+ server.update({
44
+ world: { score: 200 },
45
+ self: { public: { x: 150, y: 150 } }
46
+ });
52
47
  ```
53
48
 
54
- ## 三种消息类型
49
+ ## 数据格式
55
50
 
56
- ### 1. init() - 初始化服务器
57
-
58
- 初始化服务器(必需,连接后必须先调用)。timestamp(在构造函数中传入)比现有的大则删除旧服务器并重新创建,否则返回现有数据。
59
-
60
- ```javascript
61
- server.init({
62
- world: { score: 0, level: 1 }, // 可选:世界初始数据
63
- self: {
64
- public: { name: 'Player1', x: 100, y: 100 } // 可选:公开数据
65
- }
66
- });
67
- ```
51
+ 服务器推送的数据格式(通过 `onData` 接收):
68
52
 
69
- **响应数据(通过 onData 接收):**
70
53
  ```json
71
54
  {
72
55
  "world": { "score": 0, "level": 1 },
73
56
  "self": { "name": "Player1", "x": 100, "y": 100 },
74
57
  "players": [
75
- { "name": "Player2", "x": 10, "y": 20 }
58
+ { "name": "Player2", "x": 10, "y": 20 },
59
+ { "name": "Player3", "x": 30, "y": 40 }
76
60
  ]
77
61
  }
78
62
  ```
79
63
 
80
- **注意:** `self` 和 `players[i]` 现在具有相同的结构,都只包含公开数据。
64
+ ## API 说明
65
+
66
+ ### init(data) - 初始化服务器
81
67
 
82
- **关于 timestamp:**
83
- - **timestamp 较小或相等**:传入的值不生效,直接返回现有数据
84
- - **timestamp 较大**:删除旧服务器并重新创建,使用传入的值初始化数据
85
- - **何时更新 timestamp**:如果更改了游戏,且改动了角色或世界字段的设计(如新增/删除字段、改变字段类型),就需要使用更大的 timestamp(上下文中会给出当前编辑时的timestamp)让服务器重新初始化,避免数据结构不兼容
68
+ 必需,连接后必须先调用。根据 timestamp 判断是否重新初始化。
86
69
 
87
- **注意:** init 成功后服务器每 0.2 秒自动推送最新数据
70
+ ```javascript
71
+ server.init({
72
+ world: { score: 0, level: 1 }, // 可选:世界初始数据
73
+ self: {
74
+ public: { name: 'Player1', x: 100, y: 100 } // 可选:玩家公开数据
75
+ }
76
+ });
77
+ ```
88
78
 
89
- ### 2. update() - 更新数据
79
+ ### update(data) - 更新数据
90
80
 
91
- 部分更新数据(合并到现有数据,不替换)。
81
+ 部分更新数据(合并,不替换)。只更新提供的字段,未提供的字段保持不变。
92
82
 
93
83
  ```javascript
94
84
  // 更新世界数据
95
85
  server.update({ world: { score: 200 } });
96
86
 
97
87
  // 更新玩家数据
98
- server.update({
99
- self: {
100
- public: { x: 15, y: 25, rotation: 90 }
101
- }
102
- });
88
+ server.update({ self: { public: { x: 15, y: 25 } } });
103
89
 
104
- // 同时更新世界和玩家数据
90
+ // 同时更新
105
91
  server.update({
106
92
  world: { score: 200 },
107
93
  self: { public: { x: 15, y: 25 } }
108
94
  });
109
95
  ```
110
96
 
111
- **响应数据(通过 onData 接收):**
112
- ```json
113
- {
114
- "world": { "score": 200, "level": 1 },
115
- "self": { "name": "Player1", "x": 15, "y": 25, "rotation": 90 },
116
- "players": [{ "name": "Player2", "x": 10, "y": 20 }]
117
- }
118
- ```
119
-
120
- **注意:** 只更新提供的字段,未提供的字段保持不变,更新后立即推送最新数据
121
-
122
- ### 3. onData() - 监听数据推送
97
+ ### onData(callback) - 监听数据推送
123
98
 
124
- 监听服务器推送的最新数据。init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送。
99
+ 监听服务器推送的数据。返回取消监听函数。
125
100
 
126
101
  ```javascript
127
102
  const unsubscribe = server.onData((data) => {
128
- console.log('World:', data.world);
129
- console.log('Self:', data.self);
130
- console.log('Players:', data.players);
103
+ // 处理数据
131
104
  });
132
105
 
133
106
  // 取消监听
134
107
  unsubscribe();
135
108
  ```
136
109
 
137
- **数据格式:**
138
- ```json
139
- {
140
- "world": { "score": 200, "level": 1 },
141
- "self": { "name": "Player1", "x": 15, "y": 25 },
142
- "players": [
143
- { "name": "Player2", "x": 10, "y": 20 },
144
- { "name": "Player3", "x": 30, "y": 40 }
145
- ]
146
- }
147
- ```
148
-
149
- **注意:** `self` 和 `players[i]` 具有相同的数据结构,都只包含公开数据。
110
+ **推送时机:** init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送
150
111
 
151
- **推送时机:** init 响应、定时推送(每 0.2 秒)、init/update 后立即推送
112
+ ## 关于 timestamp
152
113
 
153
- ## 联机游戏示例
114
+ - **类型要求**:必须是数字类型(number/int),不能是字符串
115
+ - **如何获取**:直接使用上下文中给出的 timestamp 值
116
+ - **较小或相等**:不生效,返回现有数据
117
+ - **较大**:删除旧服务器并重新创建
118
+ - **何时更新**:改动角色或世界字段设计(新增/删除/改变类型)时,需使用更大的 timestamp 重新初始化
154
119
 
155
- 以下示例展示如何使用本库实现多人在线游戏:
120
+ ## 完整示例
156
121
 
157
122
  ```javascript
158
123
  import { RiffleServer } from '@lzpenguin/server';
159
124
 
160
- // post_id 和 token 会自动从浏览器 URL 参数中读取
161
125
  const server = new RiffleServer({
162
126
  url: 'wss://api.riffle.app',
163
- timestamp: 'timestamp given in the context'
127
+ timestamp: 1234567890
164
128
  });
165
129
 
166
- // 监听服务器推送,更新游戏状态
130
+ // 监听推送
167
131
  server.onData((data) => {
168
- // 更新自己的位置(self 现在直接是公开数据对象)
169
132
  const myPosition = { x: data.self.x, y: data.self.y };
170
-
171
- // 更新其他玩家列表
172
133
  const otherPlayers = data.players || [];
173
-
174
- // 渲染游戏画面
175
134
  renderPlayers(myPosition, otherPlayers);
176
135
 
177
- // 检查游戏状态
178
136
  if (data.world?.gameOver) {
179
137
  console.log('游戏结束!分数:', data.world.score);
180
138
  }
181
139
  });
182
140
 
183
- // 初始化游戏
141
+ // 初始化
184
142
  server.init({
185
143
  world: { score: 0, level: 1 },
186
- self: {
187
- public: { name: 'Player1', x: 0, y: 0 }
188
- }
144
+ self: { public: { name: 'Player1', x: 0, y: 0 } }
189
145
  });
190
146
 
191
147
  // 更新玩家位置
192
148
  function movePlayer(x, y) {
193
- server.update({
194
- self: {
195
- public: { x, y }
196
- }
197
- });
149
+ server.update({ self: { public: { x, y } } });
198
150
  }
199
151
 
200
152
  // 更新分数
201
153
  function updateScore(score) {
202
- server.update({
203
- world: { score }
204
- });
154
+ server.update({ world: { score } });
205
155
  }
206
156
  ```
207
157
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzpenguin/server",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Riffle 游戏服务器 WebSocket 客户端 SDK",
5
5
  "license": "ISC",
6
6
  "author": "lzpenguin",