-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathxhrLoadWithXDR.js
126 lines (105 loc) · 4.69 KB
/
xhrLoadWithXDR.js
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
(function functionName (_Phaser) {
var xhrLoad = _Phaser.Loader.prototype.xhrLoad;
/**
* If true and if the browser supports XDomainRequest, it will be used in preference for XHR.
*
* This is only relevant for IE 9 and should _only_ be enabled for IE 9 clients when required by the server/CDN.
*
* @property {boolean} useXDomainRequest
*/
_Phaser.Loader.prototype.useXDomainRequest = false;
/**
* @private
* @property {boolean} _warnedAboutXDomainRequest - Control number of warnings for using XDR outside of IE 9.
*/
_Phaser.Loader.prototype._warnedAboutXDomainRequest = false;
/**
* Starts the xhr loader - using XDomainRequest.
* This should _only_ be used with IE 9. Phaser does not support IE 8 and XDR is deprecated in IE 10.
*
* This is designed specifically to use with asset file processing.
*
* @method Phaser.Loader#xhrLoadWithXDR
* @private
* @param {object} file - The file/pack to load.
* @param {string} url - The URL of the file.
* @param {string} type - The xhr responseType.
* @param {function} onload - The function to call on success. Invoked in `this` context and supplied with `(file, xhr)` arguments.
* @param {function} [onerror=fileError] The function to call on error. Invoked in `this` context and supplied with `(file, xhr)` arguments.
*/
_Phaser.Loader.prototype.xhrLoadWithXDR = function (file, url, type, onload, onerror) {
// Special IE9 magic .. only
if (!this._warnedAboutXDomainRequest &&
(!this.game.device.ie || this.game.device.ieVersion >= 10))
{
this._warnedAboutXDomainRequest = true;
console.warn("Phaser.Loader - using XDomainRequest outside of IE 9");
}
// Ref: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
var xhr = new window.XDomainRequest();
xhr.open('GET', url, true);
xhr.responseType = type;
// XDomainRequest has a few quirks. Occasionally it will abort requests
// A way to avoid this is to make sure ALL callbacks are set even if not used
// More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
xhr.timeout = 3000;
onerror = onerror || this.fileError;
var _this = this;
xhr.onerror = function () {
try {
return onerror.call(_this, file, xhr);
} catch (e) {
_this.asyncComplete(file, e.message || 'Exception');
}
};
xhr.ontimeout = function () {
try {
return onerror.call(_this, file, xhr);
} catch (e) {
_this.asyncComplete(file, e.message || 'Exception');
}
};
xhr.onprogress = function() {};
xhr.onload = function () {
try {
if (xhr.readyState === 4 && xhr.status >= 400 && xhr.status <= 599) { // Handle HTTP status codes of 4xx and 5xx as errors, even if xhr.onerror was not called.
return onerror.call(_this, file, xhr);
}
else {
return onload.call(_this, file, xhr);
}
return onload.call(_this, file, xhr);
} catch (e) {
_this.asyncComplete(file, e.message || 'Exception');
}
};
file.requestObject = xhr;
file.requestUrl = url;
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
// if multiple XDomainRequests are being sent at the same time.
setTimeout(function () {
xhr.send();
}, 0);
};
/**
* Starts the xhr loader.
*
* This is designed specifically to use with asset file processing.
*
* @method Phaser.Loader#xhrLoad
* @private
* @param {object} file - The file/pack to load.
* @param {string} url - The URL of the file.
* @param {string} type - The xhr responseType.
* @param {function} onload - The function to call on success. Invoked in `this` context and supplied with `(file, xhr)` arguments.
* @param {function} [onerror=fileError] The function to call on error. Invoked in `this` context and supplied with `(file, xhr)` arguments.
*/
_Phaser.Loader.prototype.xhrLoad = function (file, url, type, onload, onerror) {
if (this.useXDomainRequest && window.XDomainRequest)
{
this.xhrLoadWithXDR(file, url, type, onload, onerror);
return;
}
xhrLoad.call(this, file, url, type, onload, onerror);
};
}(window.Phaser));