# Class NetManager

#### Hierarchy

- NetManager

- Defined in assets/libs/network/NetManager.ts:13

#####
Index

### Constructors

constructor

### Properties

_channels
_instance

### Methods

close
connect
removeNetNode
request
requestUnique
send
setNetNode
getInstance

## Constructors

### constructor

- new NetManager(): NetManager

- #### Returns NetManager

## Properties

### `Protected` _channels

_channels: {
[key: number]: NetNode ;
} = {}

#### Type declaration

- ##### [key: number]: NetNode

- Defined in assets/libs/network/NetManager.ts:15

### `Static` `Private` _instance

_instance: NetManager

- Defined in assets/libs/network/NetManager.ts:14

## Methods

### close

- close(code?: number, reason?: string, channelId?: number): void

- 节点网络断开

  ### Example

      NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);

  #### Parameters

  - ##### `Optional` code: number

    关闭码

  - ##### `Optional` reason: string

    关闭原因

  - ##### channelId: number = 0

    通道编号

  #### Returns void

  - Defined in assets/libs/network/NetManager.ts:138

### connect

- connect(options: NetConnectOptions, channelId?: number): boolean

- 网络节点连接服务器

  ### Example

      var options = {
         url: 'ws://127.0.0.1:3000',
         autoReconnect: 0            // -1 永久重连，0不自动重连，其他正整数为自动重试次数
      }
      NetManager.getInstance().connect(options, NetChannelType.Game);

  #### Parameters

  - ##### options: NetConnectOptions

    连接参数

  - ##### channelId: number = 0

    通道编号

  #### Returns boolean

  - Defined in assets/libs/network/NetManager.ts:65

### removeNetNode

- removeNetNode(channelId: number): void

- 移除Node

  #### Parameters

  - ##### channelId: number

  #### Returns void

  - Defined in assets/libs/network/NetManager.ts:50

### request

- request(reqProtocol: IRequestProtocol, rspObject: CallbackObject, showTips?: boolean, force?: boolean, channelId?: number): void

- 发起请求，并在在结果返回时调用指定好的回调函数

  ### Example

      let protocol: IRequestProtocol = {
         action: action,
         method: method,
         data: JSON.stringify(data),
         isCompress: this.isCompress,
         channelid: netConfig.channelid
      }
      return this.request(protocol, rspObject, showTips, force);

  #### Parameters

  - ##### reqProtocol: IRequestProtocol

    请求协议

  - ##### rspObject: CallbackObject

    回调对象

  - ##### showTips: boolean = true

    是否触发请求提示

  - ##### force: boolean = false

    是否强制发送

  - ##### channelId: number = 0

    通道编号

  #### Returns void

  - Defined in assets/libs/network/NetManager.ts:98

### requestUnique

- requestUnique(reqProtocol: IRequestProtocol, rspObject: CallbackObject, showTips?: boolean, force?: boolean, channelId?: number): boolean

- 同request功能一致，但在request之前会先判断队列中是否已有rspCmd，如有重复的则直接返回

  ### Example

      let protocol: IRequestProtocol = {
         action: action,
         method: method,
         data: JSON.stringify(data),
         isCompress: this.isCompress,
         channelid: netConfig.channelid
      }
      return this.request(protocol, rspObject, showTips, force);

  #### Parameters

  - ##### reqProtocol: IRequestProtocol

    请求协议

  - ##### rspObject: CallbackObject

    回调对象

  - ##### showTips: boolean = true

    是否触发请求提示

  - ##### force: boolean = false

    是否强制发送

  - ##### channelId: number = 0

    通道编号

  #### Returns boolean

  - Defined in assets/libs/network/NetManager.ts:122

### send

- send(buf: NetData, force?: boolean, channelId?: number): number

- 节点连接发送数据

  #### Parameters

  - ##### buf: NetData

  - ##### force: boolean = false

  - ##### channelId: number = 0

  #### Returns number

  - Defined in assets/libs/network/NetManager.ts:73

### setNetNode

- setNetNode(node: NetNode, channelId?: number): void

- 添加网络节点

  ### Example

      // 游戏服务器心跳协议
      class GameProtocol extends NetProtocolPako {
         // 自定义心跳协议
         getHearbeat(): NetData {
             return '{"action":"LoginAction","method":"heart","data":"null","callback":"LoginAction_heart"}';
         }
      }

      var net = new NetNodeGame();
      var ws = new WebSock();        // WebSocket 网络连接对象
      var gp = new GameProtocol();   // 网络通讯协议对象
      var gt = new NetGameTips()     // 网络提示对象
      net.init(ws, gp, gt);
      NetManager.getInstance().setNetNode(net, NetChannelType.Game);

  #### Parameters

  - ##### node: NetNode

    网络节点

  - ##### channelId: number = 0

    通道编号

  #### Returns void

  - Defined in assets/libs/network/NetManager.ts:45

### `Static` getInstance

- getInstance(): NetManager

- 网络管理单例对象

  #### Returns NetManager

  - Defined in assets/libs/network/NetManager.ts:18

Generated using TypeDoc