-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoincertainroom.js
More file actions
132 lines (119 loc) · 4.78 KB
/
joincertainroom.js
File metadata and controls
132 lines (119 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let engine=require("MatchvsDemoEngine");
let GLB=require("Glb");
let msg=require("MatvhvsMessage");
let CornetMappingHost = GLB.platform === "release"? "http://vsopen.matchvs.com":"http://alphavsopen.matchvs.com";
let shortCreate = "/extra/shortCreate?"; // 短号生成
let shortQuery = "/extra/shortQuery?"; //短号还原
cc.Class({
extends: cc.Component,
properties: {
match: cc.Node,
back: cc.Node,
nickName:cc.Label,
roomID: {
default: null,
type: cc.EditBox
},
errorHint: {
default: null,
type: cc.Label
}
},
onLoad () {
let self = this;
this.initEvent();
self.nickName.string = '用户ID:'+GLB.userID;
this.back.on('touchend',this.backa,this);
this.match.on('touchend',this.matcha,this);
},
/**
* 注册对应的事件监听和把自己的原型传递进入,用于发送事件使用
*/
initEvent:function () {
cc.systemEvent.on(msg.MATCHVS_ERROE_MSG,this.onEvent,this);
cc.systemEvent.on(msg.MATCHVS_JOIN_ROOM_RSP,this.onEvent,this);
cc.systemEvent.on(msg.MATCHVS_NETWORK_STATE_NOTIFY,this.onEvent,this);
},
/**
* 接收事件
* @param event
*/
onEvent:function (event) {
let eventData = event.data;
if (event.type === msg.MATCHVS_ERROE_MSG) {
if (eventData.errorCode === 405 ) {
this.errorHint.node.active = true;
this.errorHint.string = "房间已满";
console.warn("房间已满");
return;
}
if (eventData.errorCode === 406) {
this.errorHint.node.active = true;
this.errorHint.string = "房间已经关闭";
console.warn("房间已joinOver");
return;
}
cc.director.loadScene('lobby');
} else if (event.type === msg.MATCHVS_JOIN_ROOM_RSP) {
cc.director.loadScene("lobby");
} else if (event.type === msg.MATCHVS_NETWORK_STATE_NOTIFY) {
if (eventData.netNotify.userID === GLB.userID && eventData.netNotify.state === 1) {
console.log("netNotify.userID :"+eventData.netNotify.userID +"netNotify.state: "+eventData.netNotify.state);
cc.director.loadScene("lobby");
}
}
},
/**
* 移除监听
*/
removeEvent:function () {
cc.systemEvent.off(msg.MATCHVS_ERROE_MSG,this.onEvent);
cc.systemEvent.off(msg.MATCHVS_JOIN_ROOM_RSP,this.onEvent);
cc.systemEvent.off(msg.MATCHVS_NETWORK_STATE_NOTIFY,this.onEvent);
},
matcha:function(){
let self = this;
GLB.shortstr = self.roomID.string;
let request = new XMLHttpRequest();
let url = CornetMappingHost + shortQuery;
url += "&userID="+GLB.userID+"&mode="+1+"&sign="+this.getShortQueryUserSign(GLB.token,GLB.appKey,GLB.gameID,self.roomID.string,GLB.userID);
request.open("post",url , true);
request.setRequestHeader("Content-Type", "application/json");
request.onreadystatechange = function () {
if (request.readyState === 4 && (request.status >= 200 && request.status < 400)) {
try{
let response = request.responseText;
let data = JSON.parse(response);
GLB.roomID=data.data.longstr;
let result = engine.prototype.joinRoom(data.data.longstr, "Matchvs");
if (result === 0) {
self.errorHint.node.active = false;
} else {
self.errorHint.node.active = true;
self.errorHint.string = "没有查找到此房间,请重新输入"
}
cc.director.loadScene("createroom");
} catch(error){
console.warn(error.message);
}
}
};
let jsonParam ={
gameID:GLB.gameID,
shortstr:GLB.shortstr,
};
request.send(JSON.stringify(jsonParam));
},
backa:function(){
cc.director.loadScene("lobby");
},
onDestroy:function () {
this.removeEvent();
console.log("join certain Room 页面销毁");
},
getShortQueryUserSign:function(token,appKey,gameID,shortstr,userID) {
let signStr;
signStr = hex_md5(appKey+"&gameID="+gameID+"&shortstr="+shortstr+"&userID="+userID+"&"+token);
return signStr;
},
});