-
Creator 版本:2.4.3
-
目标平台: Android 各种机型都有测试
-
重现方式:启动helloword 直接创建一个WebSocket, 用手机CPU检测软件观察手机温度,会急剧飙升7-8度左右,关闭Socket后温度立即下降. U3D 引擎没有该问题
-
手机型号: 华为各种型号 一加8
-
重现概率:100%
HelloWord 源码
cc.Class({
extends: cc.Component,
properties: {
NetState: cc.Label,
NetStartTime: cc.Label,
Index: 1,
},
// use this for initialization
onLoad: function () {
cc.debug.setDisplayStats(true);
cc.game.setFrameRate(30);
cc.gv = {}
cc.ClassDef = {}
cc.gv.GameDefine = {}
cc.gv.GameDefine.Platform = 404;
cc.gv.GameDefine.Version = "1.1.1";
cc.gv.GameDefine.APPVersion = "2.1.1";
cc.gv.PlayerData = {}
cc.gv.PlayerData.UID = 1212960;
cc.gv.GameDefine.URL = "ws://47.92.244.236:9009/ws";
cc.ClassDef.GameWebSocket = require("GameWebSocket");
cc.gv.GameWebSocket = new cc.ClassDef.GameWebSocket();
cc.gv.MainScene = this;
this.Timer = 0;
this.Tick = null;
},
start() {
},
onOpenNetClick() {
if (!this.Tick) {
cc.gv.GameWebSocket.init();
let self = this;
this.NetState.string = "打开WebSocket"
this.Tick = setInterval(() => {
self.Timer++;
self.NetStartTime.string = "网络启动时间:" + this.Timer + " s";
}, 1000);
}
},
onSendHeartBeatClick() {
this.NetState.string = "握手并且开始发送心跳/未实现"
},
tickNetState() {
this.NetState.string = "心跳次数" + this.ID;
this.Index++;
},
closeNet(){
this.NetState.string = "关闭WebSocket"
this.NetStartTime.string = "关闭WebSocket";
this.Timer = 0;
if (this.Tick) {
clearInterval(this.Tick);
this.Tick = null;
}
},
onCloseNetClick() {
cc.gv.GameWebSocket.close();
this.closeNet();
},
});
WebSocket源码
var GameWebSocket = cc.Class({
extends: Object,
ctor: function () {
this.Socket = null;
this.IsCreateSuccess = false;
},
init: function () {
if (this.Socket) {
return;
}
var self = this;
var onSocketOpen = function (res) {
self.IsCreateSuccess = true;
};
var onSocketMessage = function (res) {
};
var onSocketClose = function (res) {
console.log("onSocketClose");
self.IsCreateSuccess = false;
if(cc.gv.MainScene){
cc.gv.MainScene.closeNet();
}
};
var onSocketError = function (res) {
console.log("onSocketError");
};
this.Socket = new WebSocket(cc.gv.GameDefine.URL);
this.Socket.binaryType = "arraybuffer";
// this.Socket.responseType = "arraybuffer";
this.Socket.onopen = onSocketOpen;
this.Socket.onmessage = onSocketMessage;
this.Socket.onerror = onSocketError;
this.Socket.onclose = onSocketClose;
},
sendMessage: function (data, needTry) {
var self = this;
if (this.Socket && this.Socket.readyState === WebSocket.OPEN) {
try {
this.Socket.send(data);
}
catch (e) {
}
}
else {
}
},
close () {
if (this.Socket) {
console.log("主动断开 socket");
this.Socket.close();
this.Socket.onopen = null;
this.Socket.onmessage = null;
this.Socket.onerror = null;
this.Socket.onclose = null;
this.Socket = null;
this.IsCreateSuccess = false;
}
}
});
module.exports = GameWebSocket;
构建安卓时候选用了sdk 26和29均有该问题
@panda能帮忙看一下么
谢谢! 立竿见影!