162 lines
4.1 KiB
JavaScript
162 lines
4.1 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('msgmanager');
|
|
var ProtoBuf = require('protobuf');
|
|
const BaseNet = require('../BaseNet');
|
|
//var MsgHandler = require("./jcmsghandler");
|
|
|
|
var MsgIDEnum = function () {
|
|
Object.defineProperty(this, 'c2sMsg', {
|
|
get: function () {
|
|
return this.msg.CMMessageId_e;
|
|
},
|
|
});
|
|
Object.defineProperty(this, 's2cMsg', {
|
|
get: function () {
|
|
return this.msg.SMMessageId_e;
|
|
},
|
|
});
|
|
|
|
this.init = function (cb) {
|
|
this.msg = null;
|
|
var protoFile = 'proto/friend/cs_msgid';
|
|
cc.loader.loadRes(
|
|
protoFile,
|
|
function (err, res) {
|
|
cc.log('loadfinish:' + protoFile);
|
|
|
|
var Builder = ProtoBuf.protoFromString(res);
|
|
|
|
this.msg = Builder.build('cs');
|
|
|
|
cb && cb(err);
|
|
}.bind(this)
|
|
);
|
|
};
|
|
};
|
|
|
|
var chatclient = function () {
|
|
this.init = function (offical, rsp) {
|
|
this.msgManager = new MsgMan();
|
|
this.msgManager.init();
|
|
this.msgIDs = new MsgIDEnum();
|
|
this.msgIDs.init(
|
|
function (err) {
|
|
this.initMsgHandler(this.msgIDs.c2sMsg, this.msgIDs.s2cMsg);
|
|
}.bind(this)
|
|
);
|
|
this.reconnectcount = 0;
|
|
this.reconnectmax = 10;
|
|
this.offical = offical;
|
|
this.rsp = rsp;
|
|
this.s2cMsgfcb = {};
|
|
};
|
|
|
|
this.initMsgHandler = function (c2sMsg, s2cMsg) {
|
|
var protoFile = 'proto/friend/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;
|
|
this.rsp.pversion = this.pversion;
|
|
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.getRelationUrl();
|
|
console.log(url);
|
|
this.msgManager.connectNet(url, this._netCallback.bind(this));
|
|
};
|
|
|
|
this.disconnect = function () {
|
|
this.reconnectcount = 9999;
|
|
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);
|
|
this.endConnectTime = new Date();
|
|
} else {
|
|
if (msg == 'close') {
|
|
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;
|
|
}
|
|
setTimeout(() => {
|
|
this.connectNet();
|
|
console.log('reconnecting');
|
|
}, stime);
|
|
var res = {
|
|
status: -1,
|
|
eventtype: 'reconnect',
|
|
};
|
|
this.rsp && this.rsp.netResponse(res);
|
|
}
|
|
};
|
|
this.sengMsg = function (pid, param) {
|
|
//pid '_CMRevive'
|
|
this.msgManager.sendMsg(this.msgIDs.c2sMsg[pid], param);
|
|
};
|
|
this.recvMsg = function (msg, mid) {
|
|
this.rsp.recvMsg && this.rsp.recvMsg(mid, msg);
|
|
};
|
|
|
|
this.checkreconnectNet = function () {
|
|
if (this.reconnectcount > 0) {
|
|
this.reconnectcount = 0;
|
|
this.reconnectNet();
|
|
}
|
|
};
|
|
};
|
|
|
|
module.exports = chatclient;
|