Socket的应用(egret)

连接网络

/**
         * 连接服务器
         * @param url
         * @param proxy
         */
        connect(url:string, proxy:string):void {
            /
            //test
            //window['WebSocket'] = false;
            //
            /
            if (!this.socket) {
                if (!Global.wsValid) {
                    this.isWSP = true;//使用HTTP跟GameServerProxy通信,Proxy跟GameServer通信
                }
                // test this.isWSP = true;
                
                if (this.isWSP || (UserInfo.enableWSProxy() && !window['WebSocket']) || UserInfo.testWSProxy()) {
                    //数据上报
                    CollectUtils.event({uid: PlayerService.instance.puid, op: 'open_wsproxy'});
                    this.socket = new HawkSocketProxy();
                    console.log("use WSProxy:" + proxy);
                    this.socket.connect(proxy, this.onSocketOpen, this.onSocketClose, this.onSocketError, this);
                    this.timeOut = new egret.Timer(10000,1);
                    this.timeOut.addEventListener(egret.TimerEvent.TIMER,this.timeOutHandler,this);
                    this.timeOut.start();
                } else {
                    this.socket = new HawkSocket();
                    this.socket.connect(url, this.onSocketOpen, this.onSocketClose, this.onSocketError, this);
                    this.timeOut = new egret.Timer(10000, 1);
                    this.timeOut.addEventListener(egret.TimerEvent.TIMER, this.timeOutHandler, this);
                    this.timeOut.start();
                }
            }
            else {
                console.log("connect socket active");
                this.send(MsgCMD.CONNECT_SUCCESS);
            }
        }
this.socket实现的connect方法:在HawkSocketProxy()中

在hawk.d.ts中声明:

declare class HawkSocket {
    getUrl() : string;
    isAlive():boolean;
    connect(url:string, open_cb:Function, close_cb:Function, msg_cb:Function,thisObject:any):void;
    sendProtocol(hpCode:any, msgBuilder:any):boolean;
    close():void;
}

declare class HawkProtocolManager {
    static registerProtocol(protocolFile:string):any;
    static registerHandler(hpCode:number, messageName:string, handler:Function,thisObject:any):boolean;
    static newBuilder(messageName:string):any;
    static getBuilderClass(messageName:string):any;
    static setType(type:string):void;
    static emptyProtocol():Buffer.HPEmpty;
}

在代码中实现空协议的方法:Message.instance.send(Code.MINE_MAIN_PAGE_C,HawkProtocolManager.emptyProtocol(),true);

Message.instance.send(Code.MINE_MAIN_PAGE_C,HawkProtocolManager.emptyProtocol(),true);   
连接成功回调:this.onSocketOpen();

断开连接回调:this.onSocketClose();

Socket异常:this.onSocketError();

连接超时回调:this.timeOutHandler();

=============================================

以上是没有webSocket时连接。当有webSocket时,使用webSocket进行连接::

webSocket连接:

this.socket = new HawkSocketProxy();
                    console.log("use WSProxy:" + proxy);
                    this.socket.connect(proxy, this.onSocketOpen, this.onSocketClose, this.onSocketError, this);
                    this.timeOut = new egret.Timer(10000,1);
                    this.timeOut.addEventListener(egret.TimerEvent.TIMER,this.timeOutHandler,this);
                    this.timeOut.start();
declare class HawkSocketProxy {
    getUrl():string;
    isAlive():boolean;
    connect(url:string, open_cb:Function, close_cb:Function, error_cb:Function, thisObject:any):void;
    sendProtocol(hpCode:any, msgBuilder:any):boolean;
    close():void;
}


egret封装了协议的方法: EgretWs.js Html5WS.js 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值