# Class MessageManager

全局消息管理

### Example

    // 注册持续监听的全局事件
    export class RoleViewComp extends Component{
       onLoad(){
           // 监听全局事件
           oops.message.on(GameEvent.GameServerConnected, this.onHandler, this);
       }

       protected onDestroy() {
           // 对象释放时取消注册的全局事件
           oops.message.off(GameEvent.GameServerConnected, this.onHandler, this);
       }

       private onHandler(event: string, args: any) {
           switch (event) {
               case GameEvent.GameServerConnected:
                   console.log("处理游戏服务器连接成功后的逻辑");
                   break;
           }
       }
    }

    // 注册只触发一次的全局事件
    export class RoleViewComp extends Component{
       onLoad(){
           // 监听一次事件，事件响应后，该监听自动移除
           oops.message.once(GameEvent.GameServerConnected, this.onHandler, this);
       }

       private onHandler(event: string, args: any) {
           switch (event) {
               case GameEvent.GameServerConnected:
                   console.log("处理游戏服务器连接成功后的逻辑");
                   break;
           }
       }
    }

#### Hierarchy

- MessageManager

- Defined in assets/core/common/event/MessageManager.ts:108

#####
Index

### Constructors

constructor

### Properties

events
Instance

### Methods

dispatchEvent
off
on
once

## Constructors

### constructor

- new MessageManager(): MessageManager

- #### Returns MessageManager

## Properties

### `Private` events

events: Map<string, EventData[]> = ...

- Defined in assets/core/common/event/MessageManager.ts:111

### `Static` `Readonly` Instance

Instance: MessageManager = ...

- Defined in assets/core/common/event/MessageManager.ts:109

## Methods

### dispatchEvent

- dispatchEvent(event: string, ...args: any): void

- 触发全局事件

  #### Parameters

  - ##### event: string

  - ##### `Rest` ...args: any

  #### Returns void

  - Defined in assets/core/common/event/MessageManager.ts:195

### off

- off(event: string, listener: Function, object: object): void

- 移除全局事件

  #### Parameters

  - ##### event: string

    事件名

  - ##### listener: Function

    处理事件的侦听器函数

  - ##### object: object

    侦听函数绑定的作用域对象

  #### Returns void

  - Defined in assets/core/common/event/MessageManager.ts:168

### on

- on(event: string, listener: ListenerFunc, object: object): void

- 注册全局事件

  #### Parameters

  - ##### event: string

    事件名

  - ##### listener: ListenerFunc

    处理事件的侦听器函数

  - ##### object: object

    侦听函数绑定的作用域对象

  #### Returns void

  - Defined in assets/core/common/event/MessageManager.ts:119

### once

- once(event: string, listener: ListenerFunc, object: object): void

- 监听一次事件，事件响应后，该监听自动移除

  #### Parameters

  - ##### event: string

    事件名

  - ##### listener: ListenerFunc

    事件触发回调方法

  - ##### object: object

    侦听函数绑定的作用域对象

  #### Returns void

  - Defined in assets/core/common/event/MessageManager.ts:153

Generated using TypeDoc