|
@@ -4,27 +4,24 @@ import { Controller } from "../core/controller";
|
|
|
import { useCtx } from "../ctx";
|
|
|
|
|
|
export class NatsController extends Controller {
|
|
|
-
|
|
|
_conn: any;
|
|
|
_isConned = false;
|
|
|
_startInit = false;
|
|
|
|
|
|
async onReady() {
|
|
|
- await this.initConn();
|
|
|
+ await this.initConn();
|
|
|
}
|
|
|
|
|
|
async initConn() {
|
|
|
if (this._startInit) return;
|
|
|
this._startInit = true;
|
|
|
|
|
|
- const {deviceCtrl} = useCtx()
|
|
|
-
|
|
|
+ const { deviceCtrl } = useCtx();
|
|
|
const profile = deviceCtrl.profile;
|
|
|
|
|
|
if (!profile.wsPort) return;
|
|
|
-
|
|
|
-
|
|
|
- const wsHost = `nats://localhost:${profile.wsPort}`
|
|
|
+
|
|
|
+ const wsHost = `nats://localhost:${profile.wsPort}`;
|
|
|
console.log("ws host=>", wsHost);
|
|
|
let ret = false;
|
|
|
|
|
@@ -40,13 +37,12 @@ export class NatsController extends Controller {
|
|
|
this._startInit = false;
|
|
|
return ret;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
async GetConn() {
|
|
|
return this._conn;
|
|
|
}
|
|
|
|
|
|
- async subscribe(subject: string, callback: any, options:any = {}) {
|
|
|
-
|
|
|
+ async subscribe(subject: string, callback: any, options: any = {}) {
|
|
|
if (!this._isConned) {
|
|
|
console.error("建立连接失败");
|
|
|
return;
|
|
@@ -63,11 +59,10 @@ export class NatsController extends Controller {
|
|
|
if (ret) {
|
|
|
const msg = JSON.parse(ret);
|
|
|
const out = await callback(msg);
|
|
|
- m.respond(sc.encode(out))
|
|
|
-
|
|
|
+ m.respond(sc.encode(out));
|
|
|
} else {
|
|
|
const out = await callback(ret);
|
|
|
- m.respond(sc.encode(out))
|
|
|
+ m.respond(sc.encode(out));
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.log(subject, "=>recieved json parse eror", ret);
|
|
@@ -101,18 +96,18 @@ export class NatsController extends Controller {
|
|
|
console.error(error);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- async publish(subject:string, value:string) {
|
|
|
-
|
|
|
- if (!this._isConned) {return}
|
|
|
+
|
|
|
+ async publish(subject: string, value: string) {
|
|
|
+ if (!this._isConned) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
const sc = StringCodec();
|
|
|
|
|
|
- return await this._conn.publish(subject, sc.encode(value));
|
|
|
+ return await this._conn.publish(subject, sc.encode(value));
|
|
|
}
|
|
|
|
|
|
async request(subject: string, data?: any, timeout?: number) {
|
|
|
-
|
|
|
const ret: { error: string; result: any } = { error: "", result: null };
|
|
|
if (!this._isConned) {
|
|
|
console.error("建立连接失败");
|