|
| 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 | +}; |
0 commit comments