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

年后上班老板找我的第一件大事:以后公司的棋牌产品不会有大的动作了;公司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. Python服务端工程师就业面试指导✍✍✍

    Python服务端工程师就业面试指导  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时 ...

  2. 2018今日头条湖北省赛【A】

    [题目链接]https://www.nowcoder.com/acm/contest/104/A 这题就是很简单的几何题..md现场推了很久的cos sin仿佛像个zz.自己都想给自己一巴掌. 题意就 ...

  3. iOS开发系列-定时器强引用问题

    概述 iOS开发中常用的定时器NSTimer.CADisplayLink. NSTimer 和 CADisplayLink 基本使用 NSTimer的创建方法有两个scheduledTimerWith ...

  4. java排序及泛型

    一.用泛型实现快排,可以传入不通类型进行排序,比如String数组,Integer数组. /** * 快速排序 * * @author chx * */ public class QuickSort ...

  5. 搜索框下面显示提示数据(数据是ajax读取)

    1.前台页面 <div style="margin: 0 auto"> <input type="text" id="wenxian ...

  6. [JZOJ4331] 【清华集训模拟】树

    题目 题目大意 给你一棵带点权的树,求将树变成一堆不相交的链,而且这些链的权值和非负的方案数. 正解 显然这道题是个\(DP\). 首先求个前缀和\(sum\). 为了后面讲述方便,我这样设:\(f_ ...

  7. 解析Mybatis入门第二天

    入门第二天 目的:使用Mybatis对数据库中的数据进行简单的操作.例如:增.删.改.查. 前言:同样是使用idea创建一个普通的maven工程(如何创建一个普通的Maven工程可以参考入门第一天的详 ...

  8. Windows 设置IP解决方案

    { super + e / windows + e Mouse Right play down network link }

  9. 尚学linux课程---4、linux网络配置及linux文件

    尚学linux课程---4.linux网络配置及linux文件 一.总结 一句话总结: linux下的etc目录是配置文件的目录,所以很多的文件配置操作都可以看到它的身影:比如 init系列命名,比如 ...

  10. System.Web.Mvc.HttpPutAttribute.cs

    ylbtech-System.Web.Mvc.HttpPutAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...