53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
|
import EventEmitter from 'eventemitter3';
|
||
|
import { IGraphicScene } from '../app';
|
||
|
import { CompleteMessageCliOption, IMessageClient } from './MessageBroker';
|
||
|
export interface MessageClientEvents {
|
||
|
connected: [ctx: any];
|
||
|
disconnected: [ctx: any];
|
||
|
error: [err: any];
|
||
|
}
|
||
|
export type HandleMessage = (data: any) => void;
|
||
|
export interface IMessageHandler {
|
||
|
/**
|
||
|
* id
|
||
|
*/
|
||
|
get App(): IGraphicScene;
|
||
|
/**
|
||
|
* 处理消息数据
|
||
|
* @param data
|
||
|
*/
|
||
|
handle(data: any): void;
|
||
|
}
|
||
|
export declare abstract class MessageClient extends EventEmitter<MessageClientEvents> implements IMessageClient {
|
||
|
options: CompleteMessageCliOption;
|
||
|
subClients: SubscriptionClient[];
|
||
|
constructor(options: CompleteMessageCliOption);
|
||
|
/**
|
||
|
* 订阅消息
|
||
|
* @param destination
|
||
|
* @param handle
|
||
|
*/
|
||
|
abstract subscribe(destination: string, handle: HandleMessage): boolean;
|
||
|
unsubscribe(destination: string): void;
|
||
|
abstract unsubscribe0(destination: string): void;
|
||
|
getOrNewSubClient(destination: string): SubscriptionClient;
|
||
|
addSubscription(destination: string, handler: IMessageHandler): void;
|
||
|
removeSubscription(destination: string, handle: IMessageHandler): void;
|
||
|
abstract get connected(): boolean;
|
||
|
abstract close(): void;
|
||
|
}
|
||
|
export declare class SubscriptionClient {
|
||
|
mc: MessageClient;
|
||
|
destination: string;
|
||
|
protocol: 'json' | 'protobuf';
|
||
|
handlers: IMessageHandler[];
|
||
|
subscripted: boolean;
|
||
|
constructor(mc: MessageClient, destination: string, protocal: 'json' | 'protobuf');
|
||
|
addHandler(handler: IMessageHandler): void;
|
||
|
removeHandler(handler: IMessageHandler): void;
|
||
|
trySubscribe(): void;
|
||
|
unsubscribe(): void;
|
||
|
handleMessage(data: any): void;
|
||
|
onDisconnect(): void;
|
||
|
}
|