@lzpenguin/server 1.1.6 → 1.1.7

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 CHANGED
@@ -14,10 +14,7 @@ npm install @lzpenguin/server
14
14
  import { RiffleServer } from '@lzpenguin/server';
15
15
 
16
16
  // 创建服务器实例
17
- // 注意:post_id 和 token 会自动从 URL 参数中读取
18
- // URL 格式:https://example.com/game.html?post_id=123456&token=your-token
19
17
  const server = new RiffleServer({
20
- url: 'api.riffle.app', // 必需:WebSocket 服务器地址,不要带wss://前缀
21
18
  timestamp: 1234567890, // 必需:游戏时间戳(数字类型),直接使用上下文中给出的值
22
19
  autoReconnect: true, // 可选:自动重连,默认 true
23
20
  reconnectInterval: 3000 // 可选:重连间隔(毫秒),默认 3000
@@ -123,7 +120,6 @@ unsubscribe();
123
120
  import { RiffleServer } from '@lzpenguin/server';
124
121
 
125
122
  const server = new RiffleServer({
126
- url: 'api.riffle.app',
127
123
  timestamp: 1234567890
128
124
  });
129
125
 
package/index.d.ts CHANGED
@@ -6,8 +6,6 @@
6
6
  * RiffleServer 构造函数选项
7
7
  */
8
8
  export interface RiffleServerOptions {
9
- /** WebSocket 服务器 URL */
10
- url: string;
11
9
  /** 游戏时间戳(用于判断是否需要重新初始化,新 timestamp 必须大于旧的才会重置) */
12
10
  timestamp: number;
13
11
  /** 是否自动重连,默认 true */
package/index.js CHANGED
@@ -36,29 +36,22 @@ if (typeof window !== 'undefined' && window.WebSocket) {
36
36
  * import { RiffleServer } from '@riffle/server';
37
37
  *
38
38
  * const server = new RiffleServer({
39
- * url: 'wss://api.riffle.app',
40
39
  * timestamp: Date.now()
41
- * // post_id 和 token 会自动从浏览器 URL 参数中读取
42
- * // 例如:https://example.com/game.html?post_id=123456&token=xxx
43
40
  * });
44
41
  *
45
- * // 监听服务器推送的最新数据
46
42
  * server.onData((data) => {
47
43
  * console.log('World:', data.world);
48
- * console.log('Self:', data.self); // self 现在直接是公开数据,与 players[i] 结构一致
44
+ * console.log('Self:', data.self);
49
45
  * console.log('Players:', data.players);
50
46
  * });
51
47
  *
52
- * // 初始化服务器(必需,连接后必须先调用)
53
48
  * server.init({
54
- * hash: 'game-version-hash-123', // 游戏版本哈希值
55
- * world: { score: 0, level: 1 }, // 世界初始数据(可选)
49
+ * world: { score: 0, level: 1 },
56
50
  * self: {
57
- * public: { name: 'Player1', x: 100, y: 100 } // 公开数据(可选)
51
+ * public: { name: 'Player1', x: 100, y: 100 }
58
52
  * }
59
53
  * });
60
54
  *
61
- * // 更新数据
62
55
  * server.update({
63
56
  * world: { score: 200 },
64
57
  * self: {
@@ -69,23 +62,32 @@ if (typeof window !== 'undefined' && window.WebSocket) {
69
62
  */
70
63
  export class RiffleServer {
71
64
  /**
72
- * @param {Object} options - 配置选项
73
- * @param {string} options.url - WebSocket 服务器 URL(例如:'wss://api.riffle.app')
65
+ * @param {Object} [options] - 配置选项
74
66
  * @param {number} options.timestamp - 游戏时间戳(用于判断是否需要重新初始化,新 timestamp 必须大于旧的才会重置)
75
67
  * @param {boolean} [options.autoReconnect=true] - 是否自动重连
76
68
  * @param {number} [options.reconnectInterval=3000] - 重连间隔(毫秒)
77
69
  */
78
- constructor(options) {
79
- const { url, timestamp, autoReconnect = true, reconnectInterval = 3000 } = options;
80
-
81
- if (!url) {
82
- throw new Error('URL is required');
83
- }
70
+ constructor(options = {}) {
71
+ const { timestamp, autoReconnect = true, reconnectInterval = 3000 } = options;
84
72
  if (!timestamp) {
85
73
  throw new Error('timestamp is required');
86
74
  }
87
75
 
88
- // 从全局变量或浏览器 URL 参数中读取 post_id 和 token
76
+ // 从全局变量读取服务地址(会去掉协议前缀)
77
+ let baseUrl;
78
+
79
+ if (isBrowser && typeof window !== 'undefined') {
80
+ baseUrl = window.__RIFFLE_BASE_URL__;
81
+ if (baseUrl) {
82
+ baseUrl = baseUrl.replace(/^(https?|http?|wss?|ws?):\/\//i, '').replace(/\/$/, '');
83
+ }
84
+ }
85
+
86
+ if (!baseUrl) {
87
+ throw new Error('Base URL not found');
88
+ }
89
+
90
+ // 从全局变量或 URL 参数中读取 post_id 和 token
89
91
  let postId;
90
92
  let token;
91
93
 
@@ -96,13 +98,13 @@ export class RiffleServer {
96
98
  }
97
99
 
98
100
  if (!postId) {
99
- throw new Error('post_id not found in browser URL parameters. Please ensure URL contains ?post_id=xxx');
101
+ throw new Error('Post ID not found');
100
102
  }
101
103
  if (!token) {
102
- throw new Error('token not found in browser URL parameters. Please ensure URL contains &token=xxx');
104
+ throw new Error('Token not found');
103
105
  }
104
106
 
105
- this.url = url;
107
+ this.url = baseUrl;
106
108
  this.postId = postId;
107
109
  this.token = token;
108
110
  this.timestamp = timestamp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzpenguin/server",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Riffle 游戏服务器 WebSocket 客户端 SDK",
5
5
  "license": "ISC",
6
6
  "author": "lzpenguin",