Update network.js

This commit is contained in:
zhujunjie 2019-03-06 14:05:03 +08:00
parent ef4482ca28
commit 34eb2ad914

View File

@ -1,328 +1,328 @@
var jsMatchvs = JCMatchVS var jsMatchvs = JCMatchVS
var jcmsghandler = JCMsgHandler var jcmsghandler = JCMsgHandler
nwm = function(){ nwm = function(){
this.response = null; this.response = null;
this.online = false; this.online = false;
this.engine = null; this.engine = null;
this.roomUserInfoList = []; this.roomUserInfoList = [];
this.frames=[]; this.frames=[];
this.inRoom = false; this.inRoom = false;
this.cleanCb = function(){ this.cleanCb = function(){
this.cbOnLogin = null; this.cbOnLogin = null;
this.cbOnCreateRoom =null; this.cbOnCreateRoom =null;
this.cbOnJoinRoom = null; this.cbOnJoinRoom = null;
this.cbjoinRoomNotify = null; this.cbjoinRoomNotify = null;
this.cbleaveRoomNotify = null; this.cbleaveRoomNotify = null;
this.cbRoomMerge = null; this.cbRoomMerge = null;
}; };
this.clean = function(){ this.clean = function(){
this.online = false; this.online = false;
this.frameIndex = 0; this.frameIndex = 0;
this.cleanCb(); this.cleanCb();
this.roomUserInfoList = []; this.roomUserInfoList = [];
if(this.engine){ if(this.engine){
this.engine.loginOut(); this.engine.loginOut();
this.engine.reconnectcount = 999; this.engine.reconnectcount = 999;
this.engine.disconnect(); this.engine.disconnect();
this.engine = null; this.engine = null;
} }
} }
this.init = function(){ this.init = function(){
var response = new jcmsghandler(); var response = new jcmsghandler();
NetWorkHandle.NetWorkManage = this; NetWorkHandle.NetWorkManage = this;
var self = this; var self = this;
response.initResponse = function(res){ response.initResponse = function(res){
if(res.status==0){ if(res.status==0){
self.engine.login(); self.engine.login();
} }
} }
response.netResponse = function(res){ response.netResponse = function(res){
if(res.status==0){ if(res.status==0){
console.log('net 成功'); console.log('net 成功');
} }
} }
response.joinRoomResponse= function(res){ response.joinRoomResponse= function(res){
// console.log(res); // console.log(res);
// if(res.status==0||res.status==-3){ // if(res.status==0||res.status==-3){
// for(var i=0;i<res.roomUserInfoList.length;i++){ // for(var i=0;i<res.roomUserInfoList.length;i++){
// var temp = res.roomUserInfoList[i]; // var temp = res.roomUserInfoList[i];
// temp.userProfile = JSON.parse(temp.userProfile); // temp.userProfile = JSON.parse(temp.userProfile);
// self.roomUserInfoList.push(temp); // self.roomUserInfoList.push(temp);
// } // }
// } // }
if(self.cbOnJoinRoom){ if(self.cbOnJoinRoom){
self.cbOnJoinRoom(res); self.cbOnJoinRoom(res);
} }
} }
response.joinRoomNotify= function(res){ response.joinRoomNotify= function(res){
// var temp = res; // var temp = res;
// temp.userProfile = JSON.parse(temp.userProfile); // temp.userProfile = JSON.parse(temp.userProfile);
// self.roomUserInfoList.push(temp); // self.roomUserInfoList.push(temp);
hideInviteMask(); hideInviteMask();
if(self.cbjoinRoomNotify){ if(self.cbjoinRoomNotify){
self.cbjoinRoomNotify(res); self.cbjoinRoomNotify(res);
} }
} }
response.loginResponse = function(res){ response.loginResponse = function(res){
if(res.status==0){ if(res.status==0){
self.online = true; self.online = true;
console.log('login成功'); console.log('login成功');
//console.log(res); //console.log(res);
} }
if(self.cbOnLogin){ if(self.cbOnLogin){
self.cbOnLogin(res);//create or join self.cbOnLogin(res);//create or join
} }
} }
response.createRoomResponse= function(res){ response.createRoomResponse= function(res){
if(res.status==0){ if(res.status==0){
console.log('createRoom成功'); console.log('createRoom成功');
console.log(res.roomID); console.log(res.roomID);
self.roomID = res.roomID; self.roomID = res.roomID;
wx.miniProgram.postMessage({ data: self.roomID }) wx.miniProgram.postMessage({ data: self.roomID })
showInviteMask(); showInviteMask();
} }
if(self.cbOnCreateRoom){ if(self.cbOnCreateRoom){
self.cbOnCreateRoom(res); self.cbOnCreateRoom(res);
} }
} }
response.sendEventResponse = function(res) { response.sendEventResponse = function(res) {
console.log('自己广播'); console.log('自己广播');
res.srcUserId = SDKManage.account_id; res.srcUserId = SDKManage.account_id;
console.log(res); console.log(res);
NetWorkHandle.process(res); NetWorkHandle.process(res);
} }
response.sendRoomEvtNotify= function(res){ response.sendRoomEvtNotify= function(res){
console.log("收到广播") console.log("收到广播")
console.log(res); console.log(res);
NetWorkHandle.process(res); NetWorkHandle.process(res);
} }
response.reconnectResponse = function(){ response.reconnectResponse = function(){
} }
response.sendFrameEventResponse = function (res) { response.sendFrameEventResponse = function (res) {
} }
response.joinOverResponse = function(res) { response.joinOverResponse = function(res) {
console.log("joinOverResponse"); console.log("joinOverResponse");
console.log(res); console.log(res);
} }
//自己离开房间回调 //自己离开房间回调
response.leaveRoomResponse = function(res) { response.leaveRoomResponse = function(res) {
console.log('自己离开房间'); console.log('自己离开房间');
console.log( res); console.log( res);
} }
//其他成员离开房间回调 //其他成员离开房间回调
response.leaveRoomNotify = function(res) { response.leaveRoomNotify = function(res) {
console.log('其他成员离开房间'); console.log('其他成员离开房间');
console.log(res); console.log(res);
for(var i = 0;i<self.roomUserInfoList.length;i++){ for(var i = 0;i<self.roomUserInfoList.length;i++){
if(self.roomUserInfoList[i].userId == res.userId){ if(self.roomUserInfoList[i].userId == res.userId){
self.roomUserInfoList.splice(i,1); self.roomUserInfoList.splice(i,1);
break; break;
} }
} }
if(self.cbleaveRoomNotify){ if(self.cbleaveRoomNotify){
self.cbleaveRoomNotify(res); self.cbleaveRoomNotify(res);
} }
} }
response.logoutResponse = function(res) { response.logoutResponse = function(res) {
console.log("登出") console.log("登出")
console.log(res); console.log(res);
} }
response.gameStartResponse= function(res) { response.gameStartResponse= function(res) {
console.log("gameStart"); console.log("gameStart");
console.log(res); console.log(res);
self.engine.joinOver(); self.engine.joinOver();
self.engine.setFrameSync(10); self.engine.setFrameSync(10);
} }
response.frameUpdate = function(res){ response.frameUpdate = function(res){
res.isnet = true; res.isnet = true;
self.frames.push(res); self.frames.push(res);
for(var i=0;i<5;i++){ for(var i=0;i<5;i++){
self.frames.push({frameIndex:res.frameIndex,isnet:false}); self.frames.push({frameIndex:res.frameIndex,isnet:false});
} }
self.frameIndex = res.frameIndex; self.frameIndex = res.frameIndex;
} }
response.setFrameSyncResponse= function(res){ response.setFrameSyncResponse= function(res){
} }
response.joinOverRoomNotify = function(res){ response.joinOverRoomNotify = function(res){
} }
response.netWorkStateNotify= function(res){ response.netWorkStateNotify= function(res){
console.log(res); console.log(res);
if(res.state ==1){ if(res.state ==1){
for(var i = 0;i<self.roomUserInfoList.length;i++){ for(var i = 0;i<self.roomUserInfoList.length;i++){
if(self.roomUserInfoList[i].userId == res.userID){ if(self.roomUserInfoList[i].userId == res.userID){
self.roomUserInfoList.splice(i,1); self.roomUserInfoList.splice(i,1);
break; break;
} }
} }
if(self.cbleaveRoomNotify){ if(self.cbleaveRoomNotify){
self.cbleaveRoomNotify(res); self.cbleaveRoomNotify(res);
} }
} }
} }
response.joinOverResponse= function(res){ response.joinOverResponse= function(res){
console.log(res); console.log(res);
} }
response.onSMSetRoomParam = function(res){ response.onSMSetRoomParam = function(res){
console.log(res); console.log(res);
} }
response.onSMRoomMergeNotify = function(res){ response.onSMRoomMergeNotify = function(res){
console.log(res); console.log(res);
self.roomUserInfoList = []; self.roomUserInfoList = [];
for(var i=0;i<res.target_room_user_info_list.length;i++){ for(var i=0;i<res.target_room_user_info_list.length;i++){
var temp = res.target_room_user_info_list[i]; var temp = res.target_room_user_info_list[i];
temp.userProfile = JSON.parse(temp.user_profile); temp.userProfile = JSON.parse(temp.user_profile);
temp.userId = temp.account_id; temp.userId = temp.account_id;
self.roomUserInfoList.push(temp); self.roomUserInfoList.push(temp);
} }
if(self.cbRoomMerge){ if(self.cbRoomMerge){
self.cbRoomMerge(res) self.cbRoomMerge(res)
} }
} }
response.destroyRoomNotify= function(res){ response.destroyRoomNotify= function(res){
self.frames = []; self.frames = [];
} }
response.onSMResetRoom= function(res){ response.onSMResetRoom= function(res){
} }
jcmsghandler.prototype.pushMsg = function(res){ jcmsghandler.prototype.pushMsg = function(res){
} }
this.response = response; this.response = response;
setInterval(function() { setInterval(function() {
self.heart(); self.heart();
}, },
10000) 10000)
}; };
this.heart = function(dt){ this.heart = function(dt){
if(this.online&&this.engine){ if(this.online&&this.engine){
this.engine.heart(); this.engine.heart();
} }
} }
this.initengine = function(){ this.initengine = function(){
this.clean(); this.clean();
var engine = new jsMatchvs(); var engine = new jsMatchvs();
engine.__classCreate(); engine.__classCreate();
engine.init(this.response, 6001, 'beta', "1003", null); engine.init(this.response, 6001, 'beta', "1003", null);
engine.setUserId((new Date()).getTime().toString()); engine.setUserId((new Date()).getTime().toString());
engine.setToken(""); engine.setToken("");
engine.reconnectmax = -1; engine.reconnectmax = -1;
this.engine = engine; this.engine = engine;
return this.engine; return this.engine;
}; };
this.leaveRoom =function(){ this.leaveRoom =function(){
if(this.engine){ if(this.engine){
this.engine.leaveRoom(); this.engine.leaveRoom();
} }
}; };
this.joinOver=function(){ this.joinOver=function(){
if(this.engine){ if(this.engine){
this.engine.joinOver(); this.engine.joinOver();
} }
}; };
this.sendRoomEvent=function(v){ this.sendRoomEvent=function(v){
if(this.engine){ if(this.engine){
this.engine.sendRoomEvent(JSON.stringify(v)); this.engine.sendRoomEvent(JSON.stringify(v));
} }
} }
this.sendFrameEvent=function(v){ this.sendFrameEvent=function(v){
if(this.engine){ if(this.engine){
this.engine.sendFrameEvent(JSON.stringify(v)); this.engine.sendFrameEvent(JSON.stringify(v));
} }
} }
this.startGame = function(){ this.startGame = function(){
var data = { var data = {
m:NetEnum.STARTGAME, m:NetEnum.STARTGAME,
} }
this.sendRoomEvent(data); this.sendRoomEvent(data);
this.playerCount = this.roomUserInfoList.length-Main.robotCount; this.playerCount = this.roomUserInfoList.length-Main.robotCount;
} }
this.enterGame =function(){ this.enterGame =function(){
var data = { var data = {
m:NetEnum.ENTERGAME, m:NetEnum.ENTERGAME,
} }
this.sendRoomEvent(data); this.sendRoomEvent(data);
} }
this.onEnter = function(){ this.onEnter = function(){
if(this.engine){ if(this.engine){
this.engine.gameStart('go'); this.engine.gameStart('go');
} }
}; };
this.gameStart= function(){ this.gameStart= function(){
this.engine.gameStart('go'); this.engine.gameStart('go');
}; };
this.sendJump= function(){ this.sendJump= function(){
var data = { var data = {
m:NetEnum.JUMP, m:NetEnum.JUMP,
} }
this.sendFrameEvent(data); this.sendFrameEvent(data);
}; };
this.useItem= function(itemid){ this.useItem= function(itemid){
var data = { var data = {
m:NetEnum.USEITEM, m:NetEnum.USEITEM,
v:itemid, v:itemid,
} }
this.sendFrameEvent(data); this.sendFrameEvent(data);
}; };
this.endItem= function(itemid){ this.endItem= function(itemid){
var data = { var data = {
m:NetEnum.ENDITEM, m:NetEnum.ENDITEM,
v:itemid, v:itemid,
} }
this.sendFrameEvent(data); this.sendFrameEvent(data);
}; };
this.changeCloth = function(cid){ this.changeCloth = function(cid){
var data = { var data = {
m:NetEnum.CHANGECLOTH, m:NetEnum.CHANGECLOTH,
v:cid v:cid
} }
this.sendRoomEvent(data); this.sendRoomEvent(data);
} }
this.startMatch = function(){ this.startMatch = function(){
var data = { var data = {
m:NetEnum.STARTMATCH, m:NetEnum.STARTMATCH,
} }
this.sendRoomEvent(data); this.sendRoomEvent(data);
} }
this.setRobot = function(c){ this.setRobot = function(c){
var data = { var data = {
m:NetEnum.SETROBOT, m:NetEnum.SETROBOT,
v:c v:c
} }
this.sendRoomEvent(data); this.sendRoomEvent(data);
} }
this.setRoomParam =function(v){ this.setRoomParam =function(v){
if(this.engine){ if(this.engine){
this.engine.setRoomParam(v); this.engine.setRoomParam(v);
} }
}; };
this.resetRoom =function(){ this.resetRoom =function(){
if(this.engine){ if(this.engine){
this.engine.resetRoom(0); this.engine.resetRoom(0);
} }
}; };
} }
NetWorkManage = new nwm(); NetWorkManage = new nwm();
NetWorkManage.init(); NetWorkManage.init();