first commit
This commit is contained in:
80
assets/scripts/network/http/HttpInvoke.ts
Normal file
80
assets/scripts/network/http/HttpInvoke.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { _decorator } from 'cc';
|
||||
import { NetEvent } from "../socket/NetInterface";
|
||||
import { EventMgr } from '../../utils/EventMgr';
|
||||
|
||||
|
||||
export enum HttpInvokeType {
|
||||
GET,
|
||||
POST
|
||||
}
|
||||
|
||||
|
||||
export class HttpInvoke {
|
||||
|
||||
protected _receiveTime: number = 15000; // 多久没收到数据断开
|
||||
protected _name:string = "";
|
||||
protected _otherData:any = null;
|
||||
|
||||
|
||||
public init(name:string,_otherData:any = null):void{
|
||||
this._name = name;
|
||||
this._otherData = _otherData;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private onComplete(data:any):void{
|
||||
var json = {};
|
||||
if(data){
|
||||
try {
|
||||
json = JSON.parse(data.responseText);
|
||||
} catch (e) {
|
||||
console.log("onComplete--e:",e)
|
||||
}
|
||||
}
|
||||
EventMgr.emit(this._name, json,this._otherData);
|
||||
EventMgr.emit(NetEvent.ServerRequestSucess,json);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public doSend(url:string,params:any,type:HttpInvokeType):void{
|
||||
var self = this;
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.timeout = this._receiveTime;
|
||||
|
||||
|
||||
console.log("doSend:",url,params,type)
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4) {
|
||||
console.log("onreadystatechange:",xhr.responseText);
|
||||
if (xhr.status >= 200 && xhr.status < 400) {
|
||||
self.onComplete(xhr);
|
||||
} else {
|
||||
self.onComplete(null);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
xhr.ontimeout = function () {
|
||||
console.log("xhr.ontimeout");
|
||||
self.onComplete(null);
|
||||
};
|
||||
xhr.onerror = function (e) {
|
||||
console.log("xhr.onerror:", xhr.readyState, xhr.status, e);
|
||||
self.onComplete(null);
|
||||
};
|
||||
|
||||
|
||||
if(type == HttpInvokeType.GET){
|
||||
url +="?"+ params;
|
||||
xhr.open("GET",url , true);
|
||||
xhr.send();
|
||||
}else if(type == HttpInvokeType.POST){
|
||||
xhr.open("POST", url, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
|
||||
xhr.send(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
assets/scripts/network/http/HttpInvoke.ts.meta
Normal file
11
assets/scripts/network/http/HttpInvoke.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "113518b9-08ac-4655-b974-09714c4ac40a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
35
assets/scripts/network/http/HttpManager.ts
Normal file
35
assets/scripts/network/http/HttpManager.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { _decorator } from 'cc';
|
||||
import { HttpInvoke,HttpInvokeType } from "./HttpInvoke";
|
||||
|
||||
export class HttpManager {
|
||||
private static _instance: HttpManager = null;
|
||||
public static getInstance(): HttpManager {
|
||||
if (this._instance == null) {
|
||||
this._instance = new HttpManager();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
|
||||
protected _url:string = "";
|
||||
public setWebUrl(url:string):void{
|
||||
if(this._url == "" || this._url != url){
|
||||
this._url = url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public doGet(name:string,apiUrl:string,params:any,otherData:any = null):void{
|
||||
var invoke = new HttpInvoke();
|
||||
invoke.init(name,otherData);
|
||||
invoke.doSend(this._url + apiUrl,params,HttpInvokeType.GET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public doPost(name:string,apiUrl:string,params:any,otherData:any = null):void{
|
||||
var invoke = new HttpInvoke();
|
||||
invoke.init(name,otherData);
|
||||
invoke.doSend(this._url + apiUrl,params,HttpInvokeType.POST);
|
||||
}
|
||||
}
|
||||
11
assets/scripts/network/http/HttpManager.ts.meta
Normal file
11
assets/scripts/network/http/HttpManager.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e31d2dab-7625-4555-b2a3-cebd6923f8da",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user