Socket的应用(egret)

来源:互联网 发布:ajax 返回json eval 编辑:程序博客网 时间:2024/06/06 04:48

连接网络

/**         * 连接服务器         * @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 





0 0
原创粉丝点击