pubgv3/assets/scripts/chat/chatrsp.js
zhuguoqing ff550d5d6a init
2022-05-22 10:32:02 +08:00

105 lines
2.8 KiB
JavaScript

var chatclient = require("chatclient")
const BaseNet = require('../BaseNet');
var rsp = function(){
this.msgpool = []
this.connecting = false
this.time = 0
this.time2 = 0
this.functionarr={}
this.deadtime = 35
this.hearttime = 10
this.initResponse = function(){
for(var k in this.fcb){
var k2 = this.m_chat.s2cMsgfcb["_"+k]
this.functionarr[k2] = this.fcb[k]
}
this.msgfcb = {}
for(var k in this.m_chat.s2cMsgfcb){
var vk = k.slice(1,k.length)
this.msgfcb[this.m_chat.s2cMsgfcb[k]]=vk
}
this.m_chat.connectNet();
}
this.netResponse = function(res){
console.log(res.eventtype)
if(res.eventtype=="connect"){
this.connecting = true
this.loginparma.proto_version = this.pversion
this.loginparma.zid = BaseNet.getCurrentZid();
//console.log("准备登录测试======",this.loginparma);
this.sendmsg("CMLogin",this.loginparma)
}else if(res.eventtype=="close"||res.eventtype=="reconnect"){
this.connecting = false
this.netclosecb()
}
}
this.recvMsg = function(mid,msg){
this.time2 = 0
if(mid==101){//心跳包无视
return
}
if(this.showdebug){
console.log(mid)
console.log(msg)
}
if(this.functionarr[mid]){
this.functionarr[mid](msg)
}else{
cc.Notifier.emit(this.msgfcb[mid],msg)
}
}
this.initengine = function(offical,showdebug){
this.m_chat = new chatclient()
this.m_chat.init(offical,this)
this.showdebug = showdebug
}
this.sendmsg = function(k,p){
this.msgpool.push({k:"_"+k,p:p})
if(this.showdebug){
// console.log(k)
if(p){
// console.log(p)
}
}
}
this.heart = function(){
this.m_chat.sengMsg('_CMPing')
}
this.update=function(dt){
if(this.connecting){
if(this.msgpool.length>0){
var msg = this.msgpool.shift()
this.m_chat.sengMsg(msg.k,msg.p)
}
this.time+=dt
if(this.time>this.hearttime){
this.time = 0
this.heart()
}
// this.time2+=dt
// if(this.time2>this.deadtime){
// this.m_chat.reconnectNet()
// this.time2 = 0
// }
}
}
this.bindrecvMsg = function(v){
this.fcb = v
}
this.bindclose = function(v){
this.netclosecb = v
}
this.close = function(){
this.m_chat.disconnect()
}
}
module.exports = rsp;