core-egret.HttpMethod.globalMember

egret.HttpMethod.globalMember 使用範例


class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();
        egret.lifecycle.onPause = () => egret.ticker.pause();
        egret.lifecycle.onResume = () => egret.ticker.resume();
        egret.registerImplementation('eui.IAssetAdapter', new AssetAdapter());
        egret.registerImplementation('eui.IThemeAdapter', new ThemeAdapter());
        this.runGame();
    }

    private async loadResource() {
        let loadingView = this.stage.addChild(new LoadingUI()) as LoadingUI;
        await RES.loadConfig('resource/default.res.json', 'resource/');
        await new Promise(resolve => new eui.Theme('resource/default.thm.json', this.stage).once(eui.UIEvent.COMPLETE, resolve, this));
        await RES.loadGroup('preload', 0, loadingView);
        this.stage.removeChild(loadingView);
    }

    private async runGame() {
        await this.loadResource();
        // --- Edwin 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        const request = new egret.HttpRequest;

        // 資料從伺服器回傳的監聽
        request.responseType = egret.HttpResponseType.TEXT; // 回傳類型為文字
        request.addEventListener(egret.Event.COMPLETE, (e) => {
            var request = e.currentTarget as egret.HttpRequest;
            egret.log(request.response); // 文字類型資料
        }, this);

        // get 方法實作
        const tf_get = this.addChild(new eui.Label) as eui.Label;
        tf_get.text = 'trigger get method by clicking me';
        tf_get.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            request.open('http://httpbin.org/get?type=get', egret.HttpMethod.GET); // get 為網址 ? 後接參數
            request.send();
        }, this);
        
        // post 方法實作
        const tf_post = this.addChild(new eui.Label) as eui.Label;
        tf_post.text = 'trigger post method by clicking me';
        tf_post.y = 60;
        tf_post.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            request.open('http://httpbin.org/post', egret.HttpMethod.POST);
            request.send(JSON.stringify({ type: 'post' })); // post 為 send 方法丟參數
        }, this);

    }

}

spacer

沒有留言:

張貼留言