坚持做一件事真的好难~ 决定重新写博客的时候想着一定要坚持一个周一篇,然而....

年后上班老板找我的第一件大事:以后公司的棋牌产品不会有大的动作了;公司PHP(内部用的运营后台)的小姐姐休产假了,我暂时接手她的工作;以后公司会向着其他游戏方向发展,想让我去学习cocos;

写了egret 半年了,伴随着今天最后一个egret棋牌游戏上线,也就宣告着我的egret生涯暂时告一段落了,今天写点东西算是纪念一下吧。

一、因为是做的h5游戏,跑在微信浏览器里面的,所以前端出身的我还是有一点点优势的,尽管我只是个菜鸟。

对于egret 锁屏js休眠的问题,由于egret的这种机制,往往会导致用户在锁屏后(或者切出去之后)再进来就会发现游戏画面渲染出现错乱,可能要等到下个阶段渲染改组件的时候才恢复;

这种问题应该也不算是新鲜事了吧。在浏览器有这样一个事件可以监听当前页面是否处于激活状态:我是判断锁屏到解锁花了多久。一般超过5秒就会让用户刷新一下,重现渲染游戏画面。

Utils.activationDoc = function (status) {
//兼容写法
var hiddenProperty = 'hidden' in document ? 'hidden' :
'webkitHidden' in document ? 'webkitHidden' :
'mozHidden' in document ? 'mozHidden' :
null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var hiddenTime = null;
var showTime = null;
var onVisibilityChange = function () {if (!document[hiddenProperty]) {
//激活做点什么
} else {
//非激活做点什么
}
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
}

二、关于websocket

一直用不习惯egret提供的socket,所以借鉴了一个小框架大家可以看一看,学习一下,啥也不说了,都在代码里

// TypeScript file
class ReconnectingWebSocket {
//These can be altered by calling code
public debug:boolean = false; //Time to wait before attempting reconnect (after close)
public reconnectInterval:number = 1000;
//Time to wait for WebSocket to open (before aborting and retrying)
public timeoutInterval:number = 2000; //Should only be used to read WebSocket readyState
public readyState:number; //Whether WebSocket was forced to close by this client
private forcedClose:boolean = false;
//Whether WebSocket opening timed out
private timedOut:boolean = false; //List of WebSocket sub-protocols
private protocols:string[] = []; //The underlying WebSocket
private ws:WebSocket;
private url:string; /**
* Setting this to true is the equivalent of setting all instances of ReconnectingWebSocket.debug to true.
*/
public static debugAll = false; //Set up the default 'noop' event handlers
public onopen:(ev:Event) => void = function (event:Event) {};
public onclose:(ev:CloseEvent) => void = function (event:CloseEvent) {};
public onconnecting:() => void = function () {};
public onmessage:(ev:MessageEvent) => void = function (event:MessageEvent) {};
public onerror:(ev:ErrorEvent) => void = function (event:ErrorEvent) {}; constructor(url:string, protocols:string[] = []) {
this.url = url;
this.protocols = protocols;
this.readyState = WebSocket.CONNECTING;
this.connect(false);
} public connect(reconnectAttempt:boolean) {
this.ws = new WebSocket(this.url, this.protocols); this.onconnecting();
this.log('ReconnectingWebSocket', 'attempt-connect', this.url); var localWs = this.ws;
var timeout = setTimeout(() => {
this.log('ReconnectingWebSocket', 'connection-timeout', this.url);
this.timedOut = true;
localWs.close();
this.timedOut = false;
}, this.timeoutInterval); this.ws.onopen = (event:Event) => {
clearTimeout(timeout);
this.log('ReconnectingWebSocket', 'onopen', this.url);
this.readyState = WebSocket.OPEN;
reconnectAttempt = false;
this.onopen(event);
}; this.ws.onclose = (event:CloseEvent) => {
clearTimeout(timeout);
this.ws = null;
if (this.forcedClose) {
this.readyState = WebSocket.CLOSED;
this.onclose(event);
} else {
this.readyState = WebSocket.CONNECTING;
this.onconnecting();
if (!reconnectAttempt && !this.timedOut) {
this.log('ReconnectingWebSocket', 'onclose', this.url);
this.onclose(event);
}
setTimeout(() => {
this.connect(true);
}, this.reconnectInterval);
}
};
this.ws.onmessage = (event) => {
this.log('ReconnectingWebSocket', 'onmessage', this.url, event.data);
this.onmessage(event);
};
this.ws.onerror = (event) => {
this.log('ReconnectingWebSocket', 'onerror', this.url, event);
this.onerror(event);
};
} public send(data:any) {
if (this.ws) {
this.log('ReconnectingWebSocket', 'send', this.url, data);
return this.ws.send(data);
} else {
throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
}
} /**
* Returns boolean, whether websocket was FORCEFULLY closed.
*/
public close():boolean {
if (this.ws) {
this.forcedClose = true;
this.ws.close();
return true;
}
return false;
} /**
* Additional public API method to refresh the connection if still open (close, re-open).
* For example, if the app suspects bad data / missed heart beats, it can try to refresh.
*
* Returns boolean, whether websocket was closed.
*/
public refresh():boolean {
if (this.ws) {
this.ws.close();
return true;
}
return false;
} private log(...args: any[]) {
if (this.debug || ReconnectingWebSocket.debugAll) {
console.debug.apply(console, args);
}
}
}

最后的egret的更多相关文章

  1. [Egret]优雅的写http

    首先,自从使用链式调用的写法后,就一发不可收拾的喜爱上了这种优雅的方式.不管是写架构还是写模块,我都会不自觉的使用这种最优雅的方式.链式写法既减少了代码量,又非常优雅的. 在使用 egret 的htt ...

  2. egret调用页面js的方法。

    参考文献: http://bbs.egret-labs.org/thread-267-3-1.html http://docs.egret-labs.org/post/manual/threelibs ...

  3. egret GUI 和 egret Wing 是我看到h5 最渣的设计

    一个抄袭FlexLite抄的连自己思想都没有,别人精髓都不懂的垃圾框架.也不学学MornUI,好歹有点自己想法. 先来个最小可用集合吧: 1. egret create legogame --type ...

  4. JS / Egret 单笔手写识别、手势识别

    UnistrokeRecognizer 单笔手写识别.手势识别 UnistrokeRecognizer : https://github.com/RichLiu1023/UnistrokeRecogn ...

  5. 自制-随机生成不重复的数组 --算法,egret平台下的TS code

    感觉这个算法经常会用到,前段时间写过一次,现在push出来.原理是有两个数组,一个数组存放随机数,然后从另一个数组提取相关的数,然后把另一个数组的大小-1,remove掉这个数,unity里也是这个原 ...

  6. Egret白鹭H5小游戏开发入门(二)

    前言: 昨天的文章中简单的介绍了Egret白鹭引擎从安装到基本的使用配置等问题,今天着重介绍H5小游戏开发的起步阶段,如Wing面板的使用,素材的处理,类的说明,开始布局等等. 整体概况: 根据上一篇 ...

  7. egret.Tween、egret.Ease

    循环调用.只能设置boolean,不能设置循环次数. egret.Tween.).call(()=>{ console.log("循环调用"); }) 每次改变时,调用onC ...

  8. Egret白鹭H5小游戏开发入门(三)

    前言: 在上一篇文章中着重介绍了H5小游戏开发的起步阶段,如Wing面板的使用,素材的处理,类的说明等等,那么今天主要是涉及到场景的创建,loading的修改等等的代码编写. 对于这一节,我在讲解的过 ...

  9. Egret白鹭H5小游戏开发入门(一)

    前言: 好久没更新博客了,以前很多都不会,所以常常写博客总结,倒是现在有点点经验了就懒了.在过去的几个月里,在canvas游戏框架方面,撸过了CreateJS,玩得了Egret,又学过PIXI.js. ...

  10. Html5 Egret游戏开发 成语大挑战(九)设置界面和声音管理

    在上一篇中,简单的使用界面元素快速实现了一个游戏中的二级页面,这种直接在游戏页面上做UI的做法并不太好,原因是,UI会让游戏的压力变大,即使它是隐蔽的,如果同样的功能在其它的地方也是一样的,那么就要写 ...

随机推荐

  1. java进行微信h5支付开发

    最近在做微信支付开发用的框架是 srpingMVC mybatis spring 下面是开发流程图 我们只需要开发红色标记的模块就可以了. 具体参数详情可以查看微信开发者文档. 新手第一次写,写的不好 ...

  2. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  3. Neo4j使用简单例子

    Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...

  4. flask-Local源码流程解析

    flask中Local源码数据类型首先明确:源码中要构造的数据类型数是这样的: __storage__ = { 用线程或者协程的唯一标识为键: {stack:[ctx(session/request) ...

  5. Lunascape:将FireFox、Safari和IE合为一体的浏览器

    转自:http://blog.bingo929.com/lunascape-firefox-safari-ie-all-in-one.html 作为前端开发/网页设计师,电脑中总是安装着各种不同内核渲 ...

  6. JDBC_事务说明

    JDBC控制事务:1.事务:一个包含多个步骤的业务操作,如果这个业务操作被事务管理则多个步骤同时成功或同时失败2.操作: 1.开启事务 2.提交事务 3.回滚事务3.使用Connection对象来管理 ...

  7. "_CMTimeGetSeconds", referenced from:

    CMTime is defined in the CoreMedia.framework. Add that framework to your project.

  8. _proto_和prototype

    _proto_和prototype 实例对象中有一个属性,_proto_,也是对象,叫原型,不是标准的属性,浏览器使用的, 构造函数中有一个属性,Prototype,也是对象,叫原型,是标准属性,程序 ...

  9. BCZM : 1.4

    书店促销活动,某套书一共有五卷.假设每一卷单独销售均需8欧元,多买则有折扣,具体折扣如下:    2 5%    3 10%    4 20%    5 25%    设计算法,计算出读者购买一批书的 ...

  10. CCPC-WFinal-女生专场

    1001:CCPC直播   字符串处理,几个if语句 1002:口算训练   前缀和处理<=根号n的因数,大于根号n的因数每个数至多有一个,用vector存下每个大因数的位置,map离散化.查询 ...