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

年后上班老板找我的第一件大事:以后公司的棋牌产品不会有大的动作了;公司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. 20140320 roc曲线 sizeof

    1.roc曲线 http://www.zhizhihu.com/html/y2012/4076.html 2.using namespace std的缺点:程序中定义一个变量cout会被误认为是std ...

  2. 2019-2020 ICPC, NERC, Northern Eurasia Finals

    A. Apprentice Learning Trajectory rdc乱编的做法 考虑贪心,每次会选择结束时间最早的. 设当前时间为 \(x\),那么可以区间有两类 a) \(l_i \leq x ...

  3. Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite.

    Petrozavodsk Summer-2016. Warsaw U Contest, XVI Open Cup Onsite. Problem A. Gambling Problem B. Colo ...

  4. 收藏的链接-English

    What is the adverb for deposit? https://www.wordhippo.com/what-is/the-adverb-for/deposit.html

  5. PHP算法之整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 3: 输入: 120输出: 21注 ...

  6. [转]Nginx配置详解

    Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也可作为 ...

  7. create table常用命令

    CREATE TABLE students( stuID INTEGER NOT NULL , stuname ) not null, sex int NOT NULL ); CREATE TABLE ...

  8. Delphi让所有的窗口的标题和图标显示在任务栏上

    Delphi:让所有的窗口的标题和图标显示在任务栏上在Delphi中,除了主窗口之外,当其它的窗口显示或切换到焦点时.默认情况下,窗口标题和图标并不会显示在任务栏中,为了实现像主窗口一样,每当窗口显示 ...

  9. 《DSP using MATLAB》Problem 8.40

    代码: function [wpLP, wsLP, alpha] = bs2lpfre(wpbs, wsbs) % Band-edge frequency conversion from bandst ...

  10. 微信小程序 主题皮肤切换(switch开关)

    示例效果: 功能点分析: 1.点击switch开关,切换主题皮肤(包括标题栏.底部tabBar):2.把皮肤设置保存到全局变量,在访问其它页面时也能有效果3.把设置保存到本地,退出应用再进来时,依然加 ...