mqtt添加发布消息
This commit is contained in:
parent
8b0ad14f73
commit
2255232499
1
lib/message/MqttBroker.d.ts
vendored
1
lib/message/MqttBroker.d.ts
vendored
@ -8,6 +8,7 @@ export declare class MqttMsgClient extends MessageClient {
|
||||
constructor(options: CompleteMessageCliOption);
|
||||
subscribe(destination: string, handle: HandleMessage): boolean;
|
||||
unsubscribe0(destination: string): void;
|
||||
publishMessage(destination: string, message: string): void;
|
||||
get connected(): boolean;
|
||||
close(): void;
|
||||
}
|
||||
|
@ -612,6 +612,10 @@ export interface IGraphicScene extends EventEmitter<GraphicAppEvents> {
|
||||
* 取消websocket订阅
|
||||
*/
|
||||
unsubscribe(destination: string): void;
|
||||
/**
|
||||
* 发布websocket消息
|
||||
*/
|
||||
publishMessage(destination: string, message: string): void;
|
||||
}
|
||||
|
||||
abstract class GraphicSceneBase
|
||||
@ -1158,6 +1162,13 @@ abstract class GraphicSceneBase
|
||||
this.checkWsMsgCli();
|
||||
this.wsMsgBroker.unsbuscribe(destination);
|
||||
}
|
||||
/**
|
||||
* 发布websocket消息
|
||||
*/
|
||||
publishMessage(destination: string, message: string) {
|
||||
this.checkWsMsgCli();
|
||||
this.wsMsgBroker.publishMessage(destination, message);
|
||||
}
|
||||
/**
|
||||
* 处理图形状态
|
||||
* @param graphicStates
|
||||
|
@ -70,6 +70,11 @@ export abstract class MessageClient
|
||||
this.getOrNewSubClient(destination).removeHandler(handle);
|
||||
}
|
||||
|
||||
publishMessage(destination: string, message: string): void {
|
||||
const cli = this.getOrNewSubClient(destination);
|
||||
cli.publishMessage(destination, message);
|
||||
}
|
||||
|
||||
abstract get connected(): boolean;
|
||||
|
||||
abstract close(): void;
|
||||
@ -125,6 +130,12 @@ export class SubscriptionClient {
|
||||
this.mc.unsubscribe(this.destination);
|
||||
}
|
||||
|
||||
publishMessage(destination: string, message: string): void {
|
||||
if (this.mc.connected) {
|
||||
this.mc.publishMessage(destination, message);
|
||||
}
|
||||
}
|
||||
|
||||
handleMessage(data: any) {
|
||||
if (this.protocol === 'json') {
|
||||
console.debug('收到消息:', data);
|
||||
|
@ -83,7 +83,12 @@ export interface IMessageClient extends EventEmitter<MessageClientEvents> {
|
||||
* @param handler
|
||||
*/
|
||||
removeSubscription(destination: string, handler: IMessageHandler): void;
|
||||
|
||||
/**
|
||||
* 发布消息
|
||||
* @param destination
|
||||
* @param message
|
||||
*/
|
||||
publishMessage(destination: string, message: string): void;
|
||||
/**
|
||||
* 是否已经连接
|
||||
*/
|
||||
@ -163,6 +168,10 @@ export class WsMsgCli {
|
||||
WsMsgCli.appMsgBroker.push(broker);
|
||||
}
|
||||
|
||||
static publishMessage(destination: string, message: string) {
|
||||
WsMsgCli.client.publishMessage(destination, message);
|
||||
}
|
||||
|
||||
static removeAppMsgBroker(broker: AppWsMsgBroker) {
|
||||
const index = WsMsgCli.appMsgBroker.findIndex((mb) => mb == broker);
|
||||
if (index >= 0) {
|
||||
@ -295,6 +304,10 @@ export class AppWsMsgBroker {
|
||||
});
|
||||
}
|
||||
|
||||
publishMessage(destination: string, message: string) {
|
||||
WsMsgCli.publishMessage(destination, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消所有订阅,从通用Stomp客户端移除此消息代理
|
||||
*/
|
||||
|
@ -112,4 +112,13 @@ export class MqttMsgClient extends MessageClient {
|
||||
console.warn('MQTT 消息客户端关闭失败', error);
|
||||
}
|
||||
}
|
||||
publishMessage(destination: string, message: string): void {
|
||||
console.debug('MQTT发布消息');
|
||||
if(this.connected) {
|
||||
this.cli.publish(destination, message);
|
||||
}else {
|
||||
console.warn('MQTT 未连接,消息发布失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,10 @@ export class StompMessagingClient extends MessageClient {
|
||||
this.cli.unsubscribe(destination);
|
||||
}
|
||||
|
||||
publishMessage(destination: string, message: string): void {
|
||||
console.debug('MQTT发布消息:未实现');
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.cli.deactivate();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user