@dd-code/uni-tools 1.0.8 → 1.0.9

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/dist/index.js CHANGED
@@ -895,7 +895,18 @@ var WsServer = /** @class */ (function () {
895
895
  this.httpServer = createHttpServer();
896
896
  this.clientMap = new Map();
897
897
  }
898
+ WsServer.getInstance = function (handleMessage) {
899
+ if (!WsServer.instance) {
900
+ WsServer.instance = new WsServer(handleMessage);
901
+ }
902
+ else if (handleMessage) {
903
+ WsServer.instance.handleMessage = handleMessage;
904
+ }
905
+ return WsServer.instance;
906
+ };
898
907
  WsServer.prototype.createServer = function () {
908
+ if (this.wss)
909
+ return; // 避免重复创建
899
910
  this.wss = new WebSocket.WebSocketServer({
900
911
  server: this.httpServer, // 绑定到 Vite 的 HTTP 服务器
901
912
  path: WS_PATH, // WS 连接路径,前端连接时用 ws://localhost:5173/__mfe__ws__
@@ -939,6 +950,7 @@ var WsServer = /** @class */ (function () {
939
950
  // 拿到客户端传递的 appCode
940
951
  var url = new URL(request.url, "http://".concat(request.headers.host));
941
952
  var appCode = url.searchParams.get("appCode");
953
+ console.log("[uni-WS] \u5BA2\u6237\u7AEF\u8FDE\u63A5 WS \u670D\u52A1\uFF0CappCode: ".concat(appCode));
942
954
  if (!appCode)
943
955
  return;
944
956
  _this.clientMap.set(appCode, ws);
@@ -959,8 +971,19 @@ var WsClientServer = /** @class */ (function () {
959
971
  this.ws = null;
960
972
  this.isConnected = false;
961
973
  }
974
+ WsClientServer.getInstance = function (handleMessage) {
975
+ if (!WsClientServer.instance) {
976
+ WsClientServer.instance = new WsClientServer(handleMessage);
977
+ }
978
+ else if (handleMessage) {
979
+ WsClientServer.instance.handleMessage = handleMessage;
980
+ }
981
+ return WsClientServer.instance;
982
+ };
962
983
  WsClientServer.prototype.connect = function (appCode) {
963
984
  var _this = this;
985
+ if (this.isConnected && this.ws)
986
+ return;
964
987
  this.ws = new WebSocket("ws://localhost:".concat(WS_PORT).concat(WS_PATH, "?appCode=").concat(appCode));
965
988
  this.ws.on("open", function () {
966
989
  // console.log("客户端已连接 WS 服务");
@@ -977,7 +1000,11 @@ var WsClientServer = /** @class */ (function () {
977
1000
  _this.isConnected = false;
978
1001
  _this.retryConnect(appCode);
979
1002
  });
980
- this.onMessage(this.handleMessage);
1003
+ // 监听消息并使用当前的 handleMessage
1004
+ this.ws.on("message", function (message) {
1005
+ var _a;
1006
+ (_a = _this.handleMessage) === null || _a === void 0 ? void 0 : _a.call(_this, JSON.parse(message.toString()));
1007
+ });
981
1008
  };
982
1009
  WsClientServer.prototype.sendMessage = function (type, data) {
983
1010
  if (this.isConnected) {
@@ -993,9 +1020,9 @@ var WsClientServer = /** @class */ (function () {
993
1020
  // // console.log("收到消息", message);
994
1021
  // };
995
1022
  WsClientServer.prototype.onMessage = function (callback) {
996
- this.ws.on("message", function (message) {
997
- callback === null || callback === void 0 ? void 0 : callback(JSON.parse(message.toString()));
998
- });
1023
+ if (callback) {
1024
+ this.handleMessage = callback;
1025
+ }
999
1026
  };
1000
1027
  WsClientServer.prototype.retryConnect = function (appCode) {
1001
1028
  var _this = this;
@@ -1122,7 +1149,7 @@ var genreNewAppJson = function (outputPageJsonPath, mainAppJsonPath, manifestLis
1122
1149
  * 处理主应用服务端逻辑
1123
1150
  */
1124
1151
  var createMainAppServer = function (manifestJson) {
1125
- var mfeServer = new WsServer(function (opt) {
1152
+ var mfeServer = WsServer.getInstance(function (opt) {
1126
1153
  var type = opt.type, data = opt.data;
1127
1154
  if (type === E_WS_TYPE.CHANGE) {
1128
1155
  var outputPageJsonPath = getMainAppJsonPath();
@@ -1145,7 +1172,7 @@ var createMainAppServer = function (manifestJson) {
1145
1172
  * 处理子应用客户端逻辑
1146
1173
  */
1147
1174
  var createMainAppClient = function (manifestJson, onInit) {
1148
- var client = new WsClientServer(function (opt) {
1175
+ var client = WsClientServer.getInstance(function (opt) {
1149
1176
  if (opt.type === E_WS_TYPE.INIT) {
1150
1177
  onInit(opt.data);
1151
1178
  }
package/dist/index.mjs.js CHANGED
@@ -893,7 +893,18 @@ var WsServer = /** @class */ (function () {
893
893
  this.httpServer = createHttpServer();
894
894
  this.clientMap = new Map();
895
895
  }
896
+ WsServer.getInstance = function (handleMessage) {
897
+ if (!WsServer.instance) {
898
+ WsServer.instance = new WsServer(handleMessage);
899
+ }
900
+ else if (handleMessage) {
901
+ WsServer.instance.handleMessage = handleMessage;
902
+ }
903
+ return WsServer.instance;
904
+ };
896
905
  WsServer.prototype.createServer = function () {
906
+ if (this.wss)
907
+ return; // 避免重复创建
897
908
  this.wss = new WebSocketServer({
898
909
  server: this.httpServer, // 绑定到 Vite 的 HTTP 服务器
899
910
  path: WS_PATH, // WS 连接路径,前端连接时用 ws://localhost:5173/__mfe__ws__
@@ -937,6 +948,7 @@ var WsServer = /** @class */ (function () {
937
948
  // 拿到客户端传递的 appCode
938
949
  var url = new URL(request.url, "http://".concat(request.headers.host));
939
950
  var appCode = url.searchParams.get("appCode");
951
+ console.log("[uni-WS] \u5BA2\u6237\u7AEF\u8FDE\u63A5 WS \u670D\u52A1\uFF0CappCode: ".concat(appCode));
940
952
  if (!appCode)
941
953
  return;
942
954
  _this.clientMap.set(appCode, ws);
@@ -957,8 +969,19 @@ var WsClientServer = /** @class */ (function () {
957
969
  this.ws = null;
958
970
  this.isConnected = false;
959
971
  }
972
+ WsClientServer.getInstance = function (handleMessage) {
973
+ if (!WsClientServer.instance) {
974
+ WsClientServer.instance = new WsClientServer(handleMessage);
975
+ }
976
+ else if (handleMessage) {
977
+ WsClientServer.instance.handleMessage = handleMessage;
978
+ }
979
+ return WsClientServer.instance;
980
+ };
960
981
  WsClientServer.prototype.connect = function (appCode) {
961
982
  var _this = this;
983
+ if (this.isConnected && this.ws)
984
+ return;
962
985
  this.ws = new WebSocket("ws://localhost:".concat(WS_PORT).concat(WS_PATH, "?appCode=").concat(appCode));
963
986
  this.ws.on("open", function () {
964
987
  // console.log("客户端已连接 WS 服务");
@@ -975,7 +998,11 @@ var WsClientServer = /** @class */ (function () {
975
998
  _this.isConnected = false;
976
999
  _this.retryConnect(appCode);
977
1000
  });
978
- this.onMessage(this.handleMessage);
1001
+ // 监听消息并使用当前的 handleMessage
1002
+ this.ws.on("message", function (message) {
1003
+ var _a;
1004
+ (_a = _this.handleMessage) === null || _a === void 0 ? void 0 : _a.call(_this, JSON.parse(message.toString()));
1005
+ });
979
1006
  };
980
1007
  WsClientServer.prototype.sendMessage = function (type, data) {
981
1008
  if (this.isConnected) {
@@ -991,9 +1018,9 @@ var WsClientServer = /** @class */ (function () {
991
1018
  // // console.log("收到消息", message);
992
1019
  // };
993
1020
  WsClientServer.prototype.onMessage = function (callback) {
994
- this.ws.on("message", function (message) {
995
- callback === null || callback === void 0 ? void 0 : callback(JSON.parse(message.toString()));
996
- });
1021
+ if (callback) {
1022
+ this.handleMessage = callback;
1023
+ }
997
1024
  };
998
1025
  WsClientServer.prototype.retryConnect = function (appCode) {
999
1026
  var _this = this;
@@ -1120,7 +1147,7 @@ var genreNewAppJson = function (outputPageJsonPath, mainAppJsonPath, manifestLis
1120
1147
  * 处理主应用服务端逻辑
1121
1148
  */
1122
1149
  var createMainAppServer = function (manifestJson) {
1123
- var mfeServer = new WsServer(function (opt) {
1150
+ var mfeServer = WsServer.getInstance(function (opt) {
1124
1151
  var type = opt.type, data = opt.data;
1125
1152
  if (type === E_WS_TYPE.CHANGE) {
1126
1153
  var outputPageJsonPath = getMainAppJsonPath();
@@ -1143,7 +1170,7 @@ var createMainAppServer = function (manifestJson) {
1143
1170
  * 处理子应用客户端逻辑
1144
1171
  */
1145
1172
  var createMainAppClient = function (manifestJson, onInit) {
1146
- var client = new WsClientServer(function (opt) {
1173
+ var client = WsClientServer.getInstance(function (opt) {
1147
1174
  if (opt.type === E_WS_TYPE.INIT) {
1148
1175
  onInit(opt.data);
1149
1176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dd-code/uni-tools",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Universal tools for code utilities",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",