mphttpx 1.0.11 → 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
@@ -54,6 +54,9 @@ This allows web code to be reused in other environments (such as mini-programs).
54
54
  - [XMLHttpRequest (mini-programs)](#xmlhttprequest-mini-programs)
55
55
  - [Example](#example-13)
56
56
  - [Compatibility](#compatibility-13)
57
+ - [WebSocket (mini-programs, since 1.1.0)](#websocket-mini-programs-since-110)
58
+ - [Example](#example-14)
59
+ - [Compatibility](#compatibility-14)
57
60
  - [Auto Import](#auto-import)
58
61
  - [UniApp \& Taro](#uniapp--taro)
59
62
  - [Node.js](#nodejs)
@@ -75,6 +78,7 @@ This allows web code to be reused in other environments (such as mini-programs).
75
78
  - **AbortController**
76
79
  - **EventTarget**
77
80
  - **XMLHttpRequest (mini-programs)**
81
+ - **WebSocket (mini-programs, since 1.1.0)**
78
82
 
79
83
  ## Installation
80
84
 
@@ -462,7 +466,7 @@ fetch("https://example.com/profile", {
462
466
  Uploading files
463
467
 
464
468
  ```javascript
465
- import { fetch, File } from "mphttpx";
469
+ import { fetch, File, FormData } from "mphttpx";
466
470
 
467
471
  const formData = new FormData();
468
472
 
@@ -818,7 +822,7 @@ xhr.send(JSON.stringify({ foo: "bar", lorem: "ipsum" }));
818
822
  Properties
819
823
 
820
824
  | Property | Available | Description |
821
- | -------- | --------- | -------------|
825
+ | --------- | --------- | -------------|
822
826
  | readyState | ✔ | 2, 3: simulated |
823
827
  | response | ✔ |
824
828
  | responseText | ✔ |
@@ -847,6 +851,54 @@ Methods
847
851
  | send(body) | ✔ |
848
852
  | setRequestHeader(header, value) | ✔ |
849
853
 
854
+ ### WebSocket (mini-programs, since 1.1.0)
855
+
856
+ #### Example
857
+
858
+ ```javascript
859
+ import { WebSocket } from "mphttpx";
860
+
861
+ // Create WebSocket connection.
862
+ const socket = new WebSocket("wss://example.com:8080");
863
+
864
+ // Change binary type from "blob" to "arraybuffer"
865
+ socket.binaryType = "arraybuffer";
866
+
867
+ // Listen for messages
868
+ socket.addEventListener("message", (event) => {
869
+ if (event.data instanceof ArrayBuffer) {
870
+ // binary frame
871
+ const view = new DataView(event.data);
872
+ console.log(view.getInt32(0));
873
+ } else {
874
+ // text frame
875
+ console.log(event.data);
876
+ }
877
+ });
878
+ ```
879
+
880
+ #### Compatibility
881
+
882
+ Properties
883
+
884
+ | Property | Available | Description |
885
+ | --------- | --------- | -------------|
886
+ | binaryType | ✔ |
887
+ | bufferedAmount | ✖ |
888
+ | extensions | ✖ |
889
+ | protocol | ✔ |
890
+ | readyState | ✔ |
891
+ | url | ✔ |
892
+
893
+ Methods
894
+
895
+ | Method | Available | Description |
896
+ | ------- | --------- | -------------|
897
+ | close() | ✔ |
898
+ | close(code) | ✔ |
899
+ | close(code, reason) | ✔ |
900
+ | send(data) | ✔ |
901
+
850
902
  ## Auto Import
851
903
 
852
904
  See [unplugin-auto-import][2] for more details.
@@ -884,6 +936,7 @@ AutoImport({
884
936
  "CustomEvent",
885
937
 
886
938
  "XMLHttpRequest", // mini-programs
939
+ "WebSocket", // mini-programs
887
940
  ],
888
941
  },
889
942
 
@@ -894,19 +947,23 @@ AutoImport({
894
947
  });
895
948
  ```
896
949
 
897
- Note for UniApp developers: If your project is a UniApp mini-program created via HBuilderX using the legacy Vue2 template,
950
+ Note for `UniApp` developers: If your project is a UniApp mini-program created via HBuilderX using the legacy Vue2 template,
898
951
  try installing an older version of the unplugin-auto-import plugin that supports CMD, such as version 0.16.7.
899
952
 
900
953
  ## UniApp & Taro
901
954
 
902
955
  ```javascript
903
956
  import { setRequest } from "mphttpx";
957
+ import { setConnectSocket } from "mphttpx";
904
958
 
905
959
  setRequest(uni.request);
906
960
  // setRequest(Taro.request);
961
+
962
+ setConnectSocket(uni.connectSocket);
963
+ // setConnectSocket(Taro.connectSocket);
907
964
  ```
908
965
 
909
- Note: When using in UniApp or Taro, if `fetch` or `XMLHttpRequest` fails to work, try explicitly setting the request function.
966
+ Note: When using in UniApp or Taro, if `fetch`, `XMLHttpRequest` or `WebSocket` fails to work, try explicitly setting the request/connectSocket function.
910
967
 
911
968
  ## Node.js
912
969