145 lines
3.7 KiB
JavaScript
145 lines
3.7 KiB
JavaScript
// Learn cc.Class:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
|
|
// Learn Attribute:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
|
|
// Learn life-cycle callbacks:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
|
|
|
|
var MsgMan = require('./net/msgmanager');
|
|
var MsgIDEnum = require('./net/msgenum');
|
|
var ProtoBuf = require('./net/proto/protobuf');
|
|
const BaseNet = require('../BaseNet');
|
|
|
|
//var MsgHandler = require("./jcmsghandler");
|
|
|
|
const state_enum = cc.Enum({
|
|
none: 0,
|
|
initing: 1,
|
|
inited: 2,
|
|
joining: 3,
|
|
joined: 4,
|
|
});
|
|
|
|
// const url_offical = 'wss://game2006';
|
|
// const url_offical2 = '.cebg.games/6001/websocket';
|
|
|
|
|
|
var JCMatchVS = function () {
|
|
this.init = function (offical, response) {
|
|
this.reconnectcount = 0;
|
|
this.reconnectmax = 5;
|
|
this.msgManager = new MsgMan();
|
|
this.msgManager.init();
|
|
this.msgIDs = new MsgIDEnum();
|
|
this.rsp = response;
|
|
this.s2cMsgfcb = {};
|
|
this.msgIDs.init(
|
|
function (err) {
|
|
this.initMsgHandler(this.msgIDs.c2sMsg, this.msgIDs.s2cMsg);
|
|
}.bind(this)
|
|
);
|
|
this.offical = offical;
|
|
};
|
|
this.initMsgHandler = function (c2sMsg, s2cMsg) {
|
|
var protoFile = 'proto/battle/cs_proto';
|
|
cc.loader.loadRes(
|
|
protoFile,
|
|
function (err, res) {
|
|
cc.log('loadfinish:' + protoFile);
|
|
var Builder = ProtoBuf.protoFromString(res);
|
|
var msg = Builder.build('cs');
|
|
this.pversion = msg.Constant_e.ProtoVersion;
|
|
|
|
for (var k in c2sMsg) {
|
|
this.msgManager.initSendMsg(
|
|
c2sMsg[k],
|
|
msg[k.slice(1, k.length)]
|
|
);
|
|
}
|
|
for (var k in s2cMsg) {
|
|
this.msgManager.initRecvMsg(
|
|
s2cMsg[k],
|
|
msg[k.slice(1, k.length)],
|
|
this.recvMsg.bind(this)
|
|
);
|
|
// this.s2cMsgfcb[s2cMsg[k]] = k
|
|
}
|
|
this.s2cMsgfcb = s2cMsg;
|
|
this.rsp && this.rsp.initResponse();
|
|
}.bind(this)
|
|
);
|
|
};
|
|
|
|
this.isConnected = function () {
|
|
return this.msgManager.isConnected();
|
|
};
|
|
this.connectNet = function () {
|
|
var url = BaseNet.getGameServer(this.team_uuid);
|
|
this.msgManager.connectNet(url, this._netCallback.bind(this));
|
|
};
|
|
this.disconnect = function () {
|
|
this.reconnectcount = 9999;
|
|
this.msgManager.closeNet();
|
|
};
|
|
this.disconnecttest = function () {
|
|
this.msgManager.closeNet();
|
|
};
|
|
this._netCallback = function (msg, err) {
|
|
if (msg == 'connect') {
|
|
|
|
this.reconnectcount = 0;
|
|
var res = {
|
|
status: 0,
|
|
eventtype: msg,
|
|
};
|
|
this.rsp && this.rsp.netResponse(res);
|
|
} else {
|
|
if (msg == 'close') {
|
|
this.reconnectNet();
|
|
}
|
|
//this.reconnectNet()
|
|
}
|
|
};
|
|
|
|
this.reconnectNet = function () {
|
|
if (this.reconnectcount > this.reconnectmax) {
|
|
var res = {
|
|
status: -1,
|
|
eventtype: 'close',
|
|
};
|
|
this.rsp && this.rsp.netResponse(res);
|
|
} else {
|
|
this.reconnectcount++;
|
|
// var stime = this.reconnectcount*1000
|
|
// if(stime>5000){
|
|
// stime = 5000
|
|
// }
|
|
if (!this.chonglianing) {
|
|
this.chonglianing = true;
|
|
setTimeout(() => {
|
|
this.chonglianing = false;
|
|
|
|
this.connectNet();
|
|
}, 3000);
|
|
}
|
|
|
|
var res = {
|
|
status: -1,
|
|
eventtype: 'reconnect',
|
|
};
|
|
this.rsp && this.rsp.netResponse(res);
|
|
}
|
|
};
|
|
this.sengMsg = function (pid, param) {
|
|
this.msgManager.sendMsg(this.msgIDs.c2sMsg[pid], param);
|
|
};
|
|
this.recvMsg = function (msg, mid) {
|
|
this.rsp.recvMsg && this.rsp.recvMsg(mid, msg);
|
|
};
|
|
};
|
|
|
|
module.exports = JCMatchVS;
|