Class NetManager

Hierarchy

  • NetManager

Constructors

Properties

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

Type declaration

_instance: NetManager

Methods

  • 节点网络断开

    Example

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

    Parameters

    • Optional code: number

      关闭码

    • Optional reason: string

      关闭原因

    • channelId: number = 0

      通道编号

    Returns void

  • 网络节点连接服务器

    Example

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

    Parameters

    Returns boolean

  • 移除Node

    Parameters

    • channelId: number

    Returns 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

  • 同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

  • 节点连接发送数据

    Parameters

    • buf: NetData
    • force: boolean = false
    • channelId: number = 0

    Returns number

  • 添加网络节点

    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

  • 网络管理单例对象

    Returns NetManager

Generated using TypeDoc