easyjssdk 1.0.14 → 1.1.0

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
@@ -10,10 +10,35 @@ A simple and easy-to-use communication SDK for WuKongIM, based on its JSON-RPC p
10
10
  npm install easyjssdk
11
11
  ```
12
12
 
13
+ ## Platform Support
14
+
15
+ | Platform | Status | Notes |
16
+ |----------|--------|-------|
17
+ | Browser | ✅ | Native WebSocket |
18
+ | Node.js | ✅ | Via `ws` (auto-installed as optional dependency) |
19
+ | WeChat Mini Program | ✅ | Uses `wx.connectSocket` |
20
+ | Alipay Mini Program | ✅ | Uses `my.connectSocket` |
21
+ | UniApp | ✅ | Uses `uni.connectSocket` |
22
+
23
+ ### WeChat Mini Program
24
+
25
+ The SDK is fully compatible with WeChat Mini Program npm build:
26
+
27
+ 1. Install the package in your mini program project:
28
+ ```bash
29
+ npm install easyjssdk
30
+ ```
31
+ 2. In WeChat DevTools, go to **Tools → Build npm**
32
+ 3. Import and use:
33
+ ```javascript
34
+ const { WKIM, WKIMChannelType, WKIMEvent } = require('easyjssdk');
35
+ ```
36
+
37
+ The `miniprogram` field in `package.json` points to the CJS build, so the mini program build tool can locate the entry correctly. The `ws` dependency is optional and will not be bundled.
38
+
13
39
  ## Usage
14
40
 
15
41
  ```typescript
16
- // ... existing code ...
17
42
  import { WKIM, WKIMChannelType, WKIMEvent } from 'easyjssdk';
18
43
 
19
44
  // 1. Initialization
@@ -30,39 +55,59 @@ im.on(WKIMEvent.Message, (message) => {
30
55
  // Process received message (message.payload, message.fromUid, etc.)
31
56
  });
32
57
 
33
- // 2.1 Receive custom event notifications (NEW!)
58
+ // 2.1 Receive custom event notifications
34
59
  im.on(WKIMEvent.CustomEvent, (event) => {
35
60
  console.log("Received event:", event);
36
61
  // Handle custom events from server
37
62
  // event = { id, type, timestamp, data }
38
63
  });
39
64
 
40
- // For more events, check the documentation
41
- https://github.com/WuKongIM/EasyJSSDK/blob/main/example/app.js#L132
65
+ // For more events, see:
66
+ // https://github.com/WuKongIM/EasyJSSDK/blob/main/example/app.js#L132
42
67
 
43
68
  // 3. Connect to the server
44
69
  await im.connect()
45
70
 
46
71
  // 4. Example: Send a message after successful connection
47
72
  const targetChannelID = "friend_user_id"; // Target user ID
48
- const messagePayload = { type: 1, content: "Hello from EasyJSSDK!" }; // Your custom message payload
73
+ const messagePayload = { type: 1, content: "Hello from EasyJSSDK!" };
49
74
  const sendResult = await im.send(targetChannelID, WKIMChannelType.Person, messagePayload);
50
75
  // sendResult.reasonCode
51
76
  ```
52
77
 
53
78
  ## Features
54
79
 
55
- - **WebSocket Communication** - Real-time bidirectional communication with WuKongIM server
56
- - **Message Sending & Receiving** - Send and receive messages with automatic acknowledgment
57
- - **Event Protocol** - Receive custom event notifications from the server (NEW!)
58
- - **Auto Reconnection** - Automatic reconnection with exponential backoff
59
- - **TypeScript Support** - Full TypeScript type definitions included
60
- - **Browser & Node.js** - Works in both browser and Node.js environments
61
- - **Singleton Mode** - Optional singleton pattern for global instance management
80
+ - **WebSocket Communication** - Real-time bidirectional communication with WuKongIM server
81
+ - **Message Sending & Receiving** - Send and receive messages with automatic acknowledgment
82
+ - **Event Protocol** - Receive custom event notifications from the server
83
+ - **Auto Reconnection** - Automatic reconnection with exponential backoff
84
+ - **TypeScript Support** - Full TypeScript type definitions included
85
+ - **Multi-Platform** - Browser, Node.js, WeChat / Alipay Mini Program, UniApp
86
+ - **Dual Module Format** - Ships both ESM and CommonJS builds
87
+ - **Singleton Mode** - Optional singleton pattern for global instance management
88
+
89
+ ## Module Formats
90
+
91
+ The package ships dual builds:
92
+
93
+ - **ESM** (`dist/esm/`) — for modern bundlers and `import` syntax
94
+ - **CJS** (`dist/cjs/`) — for `require()`, Node.js, and mini program environments
95
+
96
+ The `exports` field in `package.json` handles automatic resolution:
97
+
98
+ ```jsonc
99
+ // package.json (excerpt)
100
+ {
101
+ "main": "dist/cjs/index.js", // CJS entry
102
+ "module": "dist/esm/index.js", // ESM entry
103
+ "types": "dist/esm/index.d.ts", // TypeScript types
104
+ "miniprogram": "dist/cjs" // WeChat Mini Program entry
105
+ }
106
+ ```
62
107
 
63
108
  ## Event Protocol
64
109
 
65
- The SDK now supports the Event Protocol, allowing you to receive custom event notifications from the server:
110
+ The SDK supports the Event Protocol, allowing you to receive custom event notifications from the server:
66
111
 
67
112
  ```javascript
68
113
  // Listen for custom events
@@ -88,13 +133,13 @@ im.on(WKIMEvent.CustomEvent, (event) => {
88
133
  - `timestamp` - Event timestamp in milliseconds
89
134
  - `data` - Event payload (automatically parsed from JSON)
90
135
 
91
- 📖 **Learn More:**
136
+ See also:
92
137
  - [Event Protocol Quick Start](./docs/EVENT_PROTOCOL_QUICKSTART.md)
93
138
  - [Complete Event Protocol Documentation](./docs/EVENT_PROTOCOL.md)
94
139
  - [Event Protocol Example](./example/event-example.js)
95
140
  - [Interactive Event Test Page](./example/event-test.html)
96
141
 
97
- ## Example:
142
+ ## Example
98
143
 
99
144
  ![Example](./docs/example.png)
100
145
 
@@ -102,29 +147,21 @@ im.on(WKIMEvent.CustomEvent, (event) => {
102
147
 
103
148
  1. Clone the repository.
104
149
  2. Run `npm install`.
105
- 3. Run `npm run build` to compile TypeScript to JavaScript.
150
+ 3. Run `npm run build` to compile TypeScript (outputs both ESM and CJS).
151
+ 4. Run `npm test` to run the test suite.
106
152
 
107
153
  ## Running the Example
108
154
 
109
155
  This repository includes a simple HTML/JS example to test the SDK.
110
156
 
111
- 1. **Build the SDK:** Make sure you have built the library first:
157
+ 1. **Build the SDK:**
112
158
  ```bash
113
159
  npm run build
114
160
  ```
115
- 2. **Start a Local Server:** Navigate to the root directory of this project (`EasyJSSDK`) in your terminal. You need to serve the files using a local web server because the example uses ES Modules. A simple way is using `http-server`:
161
+ 2. **Start a local server:**
116
162
  ```bash
117
- # If you don't have http-server, install it globally:
118
163
  # npm install -g http-server
119
-
120
- # Run the server from the EasyJSSDK directory:
121
164
  http-server .
122
165
  ```
123
- Alternatively, use the VS Code "Live Server" extension or any other local server, ensuring it serves from the project root directory (`EasyJSSDK`).
124
-
125
- 3. **Open the Example:** Open your web browser and navigate to the example page, typically:
126
- `http://localhost:8080/example/`
127
- (Adjust the port number if your server uses a different one).
128
-
129
- 4. **Test:** Enter your WuKongIM server details (URL, UID, Token) and use the buttons to connect, disconnect, and send messages.
130
-
166
+ 3. **Open the example:** Navigate to `http://localhost:8080/example/`
167
+ 4. **Test:** Enter your WuKongIM server details (URL, UID, Token) and use the buttons to connect, disconnect, and send messages.