Network.js
13.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var Common = require('Common');
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// default: null, // The default value will be used only when the component attaching
// to a node for the first time
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
},
// use this for initialization
onLoad: function () {
},
statics: {
//_count: 0,
/*
var txt = document.getElementById('txt');
//1.创建XMLHttpRequest对象
var xhr = null;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//2.打开与服务器的链接
xhr.open('get',url,false);
//3.发送给服务器
xhr.send(null);
//4.响应就绪(同步请求)
var json = JSON.parse(xhr.responseText);
txt.innerHTML = json;
cc.log('其他程序');
*/
//ajax请求函数
ajax: function (strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oScope, oAdditionalData) {
try{
var xlr = null;
var oResult={};
if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc.
xlr = new XMLHttpRequest(); //Log.info('XMLHttpRequest');
} else if (window.ActiveXObject) {// code for IE6, IE5
xlr = new ActiveXObject("Microsoft.XMLHTTP"); //Log.info('ActiveXObject');
}
if (!xlr) {
cc.log("Your browser does not support XMLHTTP, using iframe instead..");
//退化到使用iframe 在cocos中无用 //这里两个函数名需要字符串输入
//iframeRequest(strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oAdditionalData)
return;
}
function onStateChange() {
// oScope._cLog.screenI("Network.ajax==>"+xlr.readyState+"\r\n");
// oScope._cLog.screenI("Network.ajax==>"+xlr.status+"\r\n");
if (xlr.readyState == 4) {// 4 = "loaded"
if (xlr.status == 200) {// 200 = OK
if(null!=oScope){
onSuccess&&onSuccess.call(oScope,xlr.responseText,oAdditionalData);
}else{
onSuccess&&onSuccess(xlr.responseText,oAdditionalData);
}
} else {
if(null!=oScope){
onError&&onError.call(oScope,xlr.responseText,oAdditionalData);
}else{
onError&&onError(xlr.responseText,oAdditionalData);
}
}
xlr=null;
}
}
strMethod = strMethod.toUpperCase();
var strDataOrPostBody = '';
if (strMethod == "GET") {
//以下这段可使用正则表达式改造
if (oDataOrPostBody) {
if (strURL.indexOf('?') > 0) { //有问号
if (strURL.indexOf('=') > strURL.indexOf('?') //并且有等号在问号右边
&& strURL.lastIndexOf('&') != strURL.length - 1) //最后一个不是&
{
strURL = strURL.concat('&');
} else { //光有问号没等号
cc.log('Unavailable Request URL!');
return;
}
} else {//没问号
strURL = strURL.concat('?');
}
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
}
strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
//strDataOrPostBody.TrimEnd('&');
} else {
cc.log( 'Unavailable Parameters!');
return;
}
}
strURL = strURL.concat(strDataOrPostBody);
}
xlr.onreadystatechange = onStateChange;
// oScope._cLog.screenI("Network.ajax==>"+strMethod+"\r\n");
// oScope._cLog.screenI("Network.ajax==>"+strURL+"\r\n");
xlr.open(strMethod, strURL, true);
var isWWWFormURLEncoded = false;
if (astrContentType) {
for (var i = 0; i < astrContentType.length; i++) {
xlr.setRequestHeader('Content-type', astrContentType[i]);
if (astrContentType[i] == 'application/x-www-form-urlencoded') {
isWWWFormURLEncoded = true;
break;
}
}
}
//xlr.setRequestHeader('Connection', 'close');
switch (strMethod) {
case 'GET':
xlr.send(null);
break;
case 'POST':
if (isWWWFormURLEncoded) {
}
if (oDataOrPostBody) {
/*
if (isWWWFormURLEncoded) {
var fd=new FormData();
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
var aParameters1=oDataOrPostBody.split('&');
for (var i=0;i<aParameters1.length;i++){
var aPairs1=aParameters1.split('=');
if (aPairs1[0] && aPairs1[1]){
fd.append(aPairs1[0],aPairs1[1]);
}
}
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
fd.append(key,oDataOrPostBody[key]);
}
//strDataOrPostBody.TrimEnd('&');
//strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
} else {
console.log("Unavailable Parameters!");
return
}
xlr.send(fd);
}else{*/
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
}
//strDataOrPostBody.TrimEnd('&');
strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
} else {
oResult.businessCode='success';
oResult.description='Unavailable Parameters!';
return oResult;
}
xlr.send(strDataOrPostBody);
//}
} else {
xlr.send(null);
}
break;
}
}catch(err){
oScope._cLog.screenI("Network.ajax error==>"+err+"\r\n");
}
},
loadImageInJSRuntime: function(strURL,iRequestId,onSuccess,onError,oScope){
cc.loader.load(
{
url:strURL,
isCrossOrigin:false,
type:'jpg' //防止跨域问题
},
function (err,texture){
if (!err) {
if(null!=oScope){
onSuccess.call(oScope,texture,iRequestId);
}else{
onSuccess(texture);
}
} else {
if(null!=oScope){
onError&&onError.call(oScope,err,iRequestId);
}else{
onError&&onError(err);
}
}
}
);
},
loadImageInNativeRuntime: function(strURL,iRequestId,onSuccess,onError,oScope,iImageRenderBatchNo){//留个钩子以便为图片加载做动画
try{
if(Common.g_oRomoteURLTexture[strURL]){
if(oScope){
onSuccess.call(oScope,Common.g_oRomoteURLTexture[strURL],iRequestId);
}else{
cc.log("无oScope");
}
}
let bLoaded = false;
cc.loader.load(strURL, Common.timeoutify(function (err, texture) {
if(!err){
texture._uuid = strURL;
//Common.g_oTextureInUsage[strURL] = texture;
if(null!=oScope){
let flag = false;
if(Common.g_strCurrentSceneName&&cc.director.getScene()&&Common.g_strCurrentSceneName == cc.director.getScene().name){
flag = true;
}
if(iImageRenderBatchNo&&iImageRenderBatchNo!=Common.g_iImageRenderBatchNo){
cc.log("Info:"+"渲染批次改变,不再继续渲染");
if (cc.sys.isNative){
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","渲染批次改变,不再继续渲染");
}
flag = false;
}
if(flag){//如果已经切换场景就不做下面这件事情
try{
onSuccess.call(oScope,texture,iRequestId);
Common.g_oRomoteURLTexture[strURL] = texture;
}catch(err){
cc.log(err);
}
}else{
cc.loader.release(texture);
cc.log("情况有变,停止请求");
}
}else{
cc.loader.release(texture);
onSuccess(texture);
}
//cc.log('Should load a texture from external url: ' + (texture instanceof cc.Texture2D));
}else{
//cc.loader.release(texture);
if(null!=oScope){
onError&&onError.call(oScope,texture,iRequestId);
}else{
onError&&onError(texture);
}
}
bLoaded = true;
},6,function () {
if(bLoaded){
return;
}
bLoaded = false;
if(null!=oScope){
onError&&onError.call(oScope,null,iRequestId);
if (cc.sys.isNative){
// jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","超时不再渲染"); //日志输入频繁 fix jerry
}
}else{
onError&&onError(texture);
}
},oScope,strURL));
}catch(err){
cc.log(err);
}
},
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});