Skip to content

Commit b282b57

Browse files
committed
Merge pull request #1541 from andot/develop
Patch XMLHttpRequest.send for ArrayBuffer & ArrayBufferView.
2 parents 18265fe + 85e205b commit b282b57

File tree

7 files changed

+166
-13
lines changed

7 files changed

+166
-13
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ frameworks/js-bindings/bindings/proj.ios_mac/build/
104104

105105
# Ignore files copied in compilation
106106
samples/*/project/proj.android/src
107+
108+
*.jsc

frameworks/js-bindings/bindings/manual/network/XMLHTTPRequest.cpp

+32-12
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,15 @@ void MinXmlHttpRequest::_setHttpRequestHeader()
172172

173173
}
174174

175-
175+
void MinXmlHttpRequest::_setHttpRequestData(const char *data, size_t len)
176+
{
177+
if (len > 0 &&
178+
(_meth.compare("post") == 0 || _meth.compare("POST") == 0 ||
179+
_meth.compare("put") == 0 || _meth.compare("PUT") == 0))
180+
{
181+
_httpRequest->setRequestData(data, len);
182+
}
183+
}
176184

177185
/**
178186
* @brief Callback for HTTPRequest. Handles the response and invokes Callback.
@@ -720,20 +728,32 @@ JS_BINDED_FUNC_IMPL(MinXmlHttpRequest, send)
720728
if (argc == 1)
721729
{
722730
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
723-
if (!args.get(0).isString())
731+
if (args.get(0).isString())
732+
{
733+
JSStringWrapper strWrap(args.get(0).toString());
734+
data = strWrap.get();
735+
_setHttpRequestData(data.c_str(), static_cast<size_t>(data.length()));
736+
}
737+
else if (args.get(0).isObject())
738+
{
739+
JSObject *obj = args.get(0).toObjectOrNull();
740+
if (JS_IsArrayBufferObject(obj))
741+
{
742+
_setHttpRequestData((const char *)JS_GetArrayBufferData(obj), JS_GetArrayBufferByteLength(obj));
743+
}
744+
else if (JS_IsArrayBufferViewObject(obj))
745+
{
746+
_setHttpRequestData((const char *)JS_GetArrayBufferViewData(obj), JS_GetArrayBufferViewByteLength(obj));
747+
}
748+
else
749+
{
750+
return false;
751+
}
752+
}
753+
else
724754
{
725755
return false;
726756
}
727-
JSStringWrapper strWrap(args.get(0).toString());
728-
data = strWrap.get();
729-
}
730-
731-
732-
if (data.length() > 0 &&
733-
(_meth.compare("post") == 0 || _meth.compare("POST") == 0 ||
734-
_meth.compare("put") == 0 || _meth.compare("PUT") == 0))
735-
{
736-
_httpRequest->setRequestData(data.c_str(), static_cast<unsigned int>(data.length()));
737757
}
738758

739759
_setHttpRequestHeader();

frameworks/js-bindings/bindings/manual/network/XMLHTTPRequest.h

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class MinXmlHttpRequest : public cocos2d::Ref
9292
void _gotHeader(std::string header);
9393
void _setRequestHeader(const char* field, const char* value);
9494
void _setHttpRequestHeader();
95+
void _setHttpRequestData(const char *data, size_t len);
9596
void _sendRequest(JSContext *cx);
9697
void _notify(JSObject * callback);
9798

samples/js-tests/project.json

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
"src/CocoStudioTest/SceneTest/SceneEditorTest.js",
177177
"src/CocoStudioTest/CocoStudioTest.js",
178178
"src/XHRTest/XHRTest.js",
179+
"src/XHRTest/XHRArrayBufferTest.js",
179180

180181
"src/Box2dTest/Box2dTest.js",
181182
"src/ChipmunkTest/ChipmunkTest.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/****************************************************************************
2+
Copyright (c) 2008-2010 Ricardo Quesada
3+
Copyright (c) 2011-2012 cocos2d-x.org
4+
Copyright (c) 2013-2014 Chukong Technologies Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
//some utils functions
28+
function ensureLeftAligned (label) {
29+
label.anchorX = 0;
30+
label.anchorY = 1;
31+
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
32+
}
33+
34+
function streamXHREventsToLabel ( xhr, label, textbox, method ) {
35+
// Simple events
36+
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
37+
xhr["on" + eventname] = function () {
38+
label.string += "\nEvent : " + eventname
39+
}
40+
});
41+
42+
// Special event
43+
xhr.onreadystatechange = function () {
44+
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
45+
var httpStatus = xhr.statusText;
46+
var response = xhr.responseText.substring(0, 100) + "...";
47+
textbox.string = method + " Response (100 chars):\n"
48+
textbox.string += response
49+
label.string += "\nStatus: Got " + method + " response! " + httpStatus
50+
}
51+
}
52+
}
53+
54+
55+
var XHRArrayBufferTestScene = TestScene.extend({
56+
ctor:function () {
57+
this._super(true);
58+
var xhrLayer = new XHRArrayBufferTestLayer();
59+
this.addChild(xhrLayer);
60+
},
61+
runThisTest:function () {
62+
cc.director.runScene(this);
63+
},
64+
MainMenuCallback:function (sender) {
65+
this._super(sender);
66+
}
67+
});
68+
69+
var XHRArrayBufferTestLayer = cc.Layer.extend({
70+
ctor:function () {
71+
this._super();
72+
},
73+
74+
onEnter: function() {
75+
this._super();
76+
var l = new cc.LabelTTF("Get infos via XHR", "Thonburi", 16);
77+
this.addChild(l, 1);
78+
l.x = winSize.width / 2;
79+
l.y = winSize.height - 60;
80+
81+
this.sendPostArrayBuffer();
82+
},
83+
84+
sendPostArrayBuffer: function() {
85+
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
86+
this.addChild(statusPostLabel, 1);
87+
88+
statusPostLabel.x = 10;
89+
statusPostLabel.y = winSize.height - 100;
90+
ensureLeftAligned(statusPostLabel);
91+
statusPostLabel.setString("Status: Send Post Request to httpbin.org with ArrayBuffer");
92+
93+
94+
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
95+
this.addChild(responseLabel, 1);
96+
ensureLeftAligned(responseLabel);
97+
responseLabel.x = 10;
98+
responseLabel.y = winSize.height / 2;
99+
100+
var xhr = cc.loader.getXMLHttpRequest();
101+
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
102+
103+
xhr.open("POST", "http://httpbin.org/post");
104+
//set Content-type "text/plain" to post ArrayBuffer or ArrayBufferView
105+
xhr.setRequestHeader("Content-Type","text/plain");
106+
// Uint8Array is an ArrayBufferView
107+
xhr.send(new Uint8Array([1,2,3,4,5]));
108+
},
109+
110+
scrollViewDidScroll:function (view) {
111+
},
112+
scrollViewDidZoom:function (view) {
113+
}
114+
});
115+
116+
var runXHRArrayBufferTest = function () {
117+
var pScene = new cc.Scene();
118+
var pLayer = new XHRArrayBufferTestLayer();
119+
pScene.addChild(pLayer);
120+
cc.director.runScene(pScene);
121+
};

samples/js-tests/src/XHRTest/XHRTest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,4 @@ var runXHRTest = function () {
178178
var pLayer = new XHRTestLayer();
179179
pScene.addChild(pLayer);
180180
cc.director.runScene(pScene);
181-
};
181+
};

samples/js-tests/src/tests-main.js

+8
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,14 @@ var testNames = [
673673
testScene:function () {
674674
return new XHRTestScene();
675675
}
676+
},
677+
{
678+
title:"XMLHttpRequest send ArrayBuffer",
679+
platforms: PLATFORM_ALL,
680+
linksrc:"src/XHRTest/XHRArrayBufferTest.js",
681+
testScene:function () {
682+
return new XHRArrayBufferTestScene();
683+
}
676684
}
677685

678686
//"UserDefaultTest",

0 commit comments

Comments
 (0)