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
- Socket的应用(egret)
- Egret开发《消灭方块》后记(二)对象池的应用
- NodeJs+Egret制作socket在线五子棋游戏(1)
- NodeJs+Egret制作socket在线五子棋游戏(2)
- Egret)fps个参数的意义
- Egret)tween的onChange事件写法
- Egret)连续tween的实现
- 每周学一点 egret(5):Egret Wing2.1 改善的地方
- Egret)egret.Bitmap对象设置透明度0的区域点击可穿透
- 【Egret】 WebSocket 的使用说明
- Egret引擎的使用
- egret写的tips
- Egret)encodeURI
- Egret)enum写法
- Egret)判断系统
- Egret)Demo - 抽奖
- Egret踩坑(一)
- egret进度条(简单)
- 谈谈MVC模式
- 了解详情
- meta标记用于定义文件信息
- (暴力55分+卡常95分补全版)NOIP 2015 D2 T3运输计划
- k近邻算法(kNN):简单粗暴
- Socket的应用(egret)
- Machine Learning学习记录 第一天
- 基于axis的webService的创建
- 1426: 收集邮票
- 电影
- jquery橡皮擦插件使用 图片适配屏幕大小
- B. Cormen — The Best Friend Of a Man
- Goods transportation
- unix环境高级编程第八章读书笔记