natsController.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { queenApi } from "queenjs";
  2. import { connect, StringCodec, Empty, ErrorCode } from "nats.ws";
  3. export class BusController {
  4. _params = new URLSearchParams(decodeURIComponent(location.search));
  5. _conn: any;
  6. _isConned = false;
  7. _startInit = false;
  8. getQuery(name: string): string {
  9. return this._params.get(name) as string;
  10. }
  11. async init(host?: string) {
  12. if (this._startInit) return;
  13. this._startInit = true;
  14. queenApi.showLoading("服务连接中...");
  15. const wsHost = host ? host : this.getQuery("host");
  16. console.log("ws host=>", wsHost)
  17. let ret = false;
  18. try {
  19. this._conn = await connect({ servers: wsHost });
  20. this._isConned = !!this._conn;
  21. ret = true;
  22. } catch (error) {
  23. console.log(error);
  24. queenApi.messageError("连接失败!");
  25. }
  26. queenApi.hideLoading();
  27. this._startInit = false;
  28. return ret;
  29. }
  30. async subscribe(subject: string, callback: any) {
  31. if (!this._isConned) {
  32. await this.init();
  33. }
  34. if (!this._isConned) {
  35. console.error("建立连接失败");
  36. return;
  37. }
  38. const sc = StringCodec();
  39. const sub = this._conn.subscribe(subject);
  40. console.log(sub);
  41. (async () => {
  42. for await (const m of sub) {
  43. console.log(sub);
  44. const ret = sc.decode(m.data);
  45. console.log(subject, "=>recieved");
  46. try {
  47. if (ret) {
  48. const msg = JSON.parse(ret);
  49. callback(msg);
  50. } else {
  51. callback(ret);
  52. }
  53. } catch (error) {
  54. console.log(subject, "=>recieved json parse eror", ret);
  55. console.log(error);
  56. }
  57. }
  58. console.log(subject, "subscription closed");
  59. })();
  60. return function () {
  61. sub.unsubscribe();
  62. };
  63. }
  64. async requestApi(subject: string, data?: any, timeout?: number) {
  65. const ret = await this.request(subject, data, timeout)
  66. console.log("request api=>", ret)
  67. if (ret.error || (ret.result.ErrorNo && ret.result.ErrorNo != 200) ) {
  68. queenApi.messageError(ret.error || ret.result.ErrorDesc );
  69. return
  70. }
  71. try {
  72. const retJson = ret.result.Result;
  73. if (!retJson) return;
  74. if (retJson[0] != '{' && retJson[0] != '[') return retJson;
  75. return JSON.parse(retJson)
  76. } catch (error) {
  77. console.log( ret );
  78. console.error(error);
  79. }
  80. }
  81. RequestWebView(name: string, data:any) {
  82. return new Promise((r)=>{
  83. let cancel:any = null;
  84. this.subscribe("webview.close."+name, function(payload:any){
  85. cancel&&cancel();
  86. r(payload)
  87. }).then(c=>{
  88. cancel = c;
  89. })
  90. this.requestApi("webview.open", {...data, name})
  91. })
  92. }
  93. async request(subject: string, data?: any, timeout?: number) {
  94. if (!this._isConned) {
  95. await this.init();
  96. }
  97. const ret: { error: string; result: any } = { error: "", result: null };
  98. if (!this._isConned) {
  99. console.error("建立连接失败");
  100. ret.error = "建立连接失败";
  101. queenApi.showConfirm({
  102. title: "数据请求失败",
  103. content: "请求数据失败,请重新启动后再试",
  104. type: "danger",
  105. });
  106. return ret;
  107. }
  108. const sc = StringCodec();
  109. try {
  110. let req = Empty;
  111. if (data) {
  112. if (typeof data != "string") {
  113. req = sc.encode(JSON.stringify(data));
  114. } else {
  115. req = sc.encode(data);
  116. }
  117. }
  118. const options = { timeout: 5000 };
  119. if (timeout) {
  120. options.timeout = timeout;
  121. }
  122. const m = await this._conn.request(subject, req, options);
  123. let payload = sc.decode(m.data);
  124. try {
  125. payload = JSON.parse(payload);
  126. console.log("m=>", payload);
  127. } catch (error) {
  128. console.log(error);
  129. }
  130. ret.result = payload;
  131. } catch (error: any) {
  132. console.error(error);
  133. ret.error = error.message || "请求" + subject + "出错";
  134. if (ret.error == "503") {
  135. //NoResponders
  136. ret.error = "网路异常,请重新服务";
  137. }
  138. }
  139. return ret;
  140. }
  141. close() {
  142. if (!this._isConned) {
  143. return;
  144. }
  145. return this._conn.close();
  146. }
  147. async Public(subject:string, data:any) {
  148. if (!this._isConned) {
  149. await this.init();
  150. }
  151. const ret: { error: string; result: any } = { error: "", result: null };
  152. if (!this._isConned) {
  153. console.error("建立连接失败");
  154. ret.error = "建立连接失败";
  155. queenApi.showConfirm({
  156. title: "数据请求失败",
  157. content: "请求数据失败,请重新启动后再试",
  158. type: "danger",
  159. });
  160. return false;
  161. }
  162. const sc = StringCodec();
  163. try {
  164. let req = Empty;
  165. if (data) {
  166. if (typeof data != "string") {
  167. req = sc.encode(JSON.stringify(data));
  168. } else {
  169. req = sc.encode(data);
  170. }
  171. }
  172. this._conn.publish(subject,req);
  173. } catch (error: any) {
  174. console.log( error);
  175. return false
  176. }
  177. return true;
  178. }
  179. }