|
@@ -0,0 +1,133 @@
|
|
|
+import { sys } from "cc";
|
|
|
+import { DEBUG } from "cc/env";
|
|
|
+
|
|
|
+export default class HTTPS {
|
|
|
+ private static _instance = null;
|
|
|
+ public static get Instance(): HTTPS {
|
|
|
+ if (this._instance == null) {
|
|
|
+ this._instance = new HTTPS();
|
|
|
+ }
|
|
|
+ return this._instance;
|
|
|
+ }
|
|
|
+ token = 'ffb5293a-37ce-4123-a074-df4b1450a2b6';
|
|
|
+
|
|
|
+ //得到现在的时间
|
|
|
+ private GetNowTime(): string {
|
|
|
+ const date = new Date(sys.now());
|
|
|
+ const hours = date.getHours();
|
|
|
+ const minutes = date.getMinutes();
|
|
|
+ const seconds = date.getSeconds();
|
|
|
+ return `${hours}:${minutes}:${seconds} `;
|
|
|
+ }
|
|
|
+ post(action: string, data: object | string): Promise<any> {
|
|
|
+ if (DEBUG) {
|
|
|
+ console.log(this.GetNowTime() + "发送消息 >>>>>> :" + action, data);
|
|
|
+ }
|
|
|
+ const promise: Promise<any> = new Promise((resolve, reject) => {
|
|
|
+ var timeoutKey: number;
|
|
|
+ var retry = 5;//失败重试次数
|
|
|
+ const send = () => {
|
|
|
+ const httpRequest = new XMLHttpRequest();
|
|
|
+ const method = 'POST'
|
|
|
+ httpRequest.onreadystatechange = (event: Event): void => {
|
|
|
+ const readyState = httpRequest.readyState;
|
|
|
+ if (readyState !== 4) return;
|
|
|
+ if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
|
|
|
+ clearTimeout(timeoutKey);
|
|
|
+ timeoutKey = null;
|
|
|
+ const requestStatus = httpRequest.status;
|
|
|
+ if (requestStatus >= 200 && requestStatus < 400) {
|
|
|
+ let resp = JSON.parse(httpRequest.responseText);
|
|
|
+ resolve(resp);
|
|
|
+ if (DEBUG) {
|
|
|
+ console.log(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (retry-- > 0) send();
|
|
|
+ else reject(new Error('全部超时'));
|
|
|
+ }
|
|
|
+
|
|
|
+ httpRequest.open(method, Url + action, true);
|
|
|
+ // httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ httpRequest.setRequestHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
+ httpRequest.setRequestHeader("token", this.token != null ? this.token : "");
|
|
|
+
|
|
|
+ var text = typeof (data) == "string" ? data : JSON.stringify(data);
|
|
|
+ data ? httpRequest.send(text) : httpRequest.send();
|
|
|
+ timeoutKey = setTimeout(() => {
|
|
|
+ timeoutKey = null;
|
|
|
+ httpRequest.abort();
|
|
|
+ reject(new Error(`Timeout`));
|
|
|
+ }, 12000);//超时时间(毫秒)
|
|
|
+ }
|
|
|
+ send();
|
|
|
+ })
|
|
|
+ return promise;
|
|
|
+ }
|
|
|
+ get(action: string): Promise<any> {
|
|
|
+
|
|
|
+ if (DEBUG) {
|
|
|
+ console.log(this.GetNowTime() + "发送消息 >>>>>> :" + action);
|
|
|
+ }
|
|
|
+
|
|
|
+ const promise: Promise<any> = new Promise((resolve, reject) => {
|
|
|
+ var timeoutKey: number;
|
|
|
+ var retry = 5;//失败重试次数
|
|
|
+ const send = () => {
|
|
|
+ const httpRequest = new XMLHttpRequest();
|
|
|
+ const method = 'GET'
|
|
|
+ httpRequest.onreadystatechange = (event: Event): void => {
|
|
|
+ const readyState = httpRequest.readyState;
|
|
|
+ if (readyState !== 4) return;
|
|
|
+ if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
|
|
|
+ clearTimeout(timeoutKey);
|
|
|
+ timeoutKey = null;
|
|
|
+ const requestStatus = httpRequest.status;
|
|
|
+ if (requestStatus >= 200 && requestStatus < 400) {
|
|
|
+ let resp = JSON.parse(httpRequest.responseText);
|
|
|
+ resolve(resp);
|
|
|
+ if (DEBUG) {
|
|
|
+ console.log(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (retry-- > 0) send();
|
|
|
+ else reject(new Error('全部超时'));
|
|
|
+ }
|
|
|
+
|
|
|
+ httpRequest.open(method, action, true);
|
|
|
+ httpRequest.setRequestHeader("token", this.token != null ? this.token : "");
|
|
|
+ httpRequest.send();
|
|
|
+ timeoutKey = setTimeout(() => {
|
|
|
+ timeoutKey = null;
|
|
|
+ httpRequest.abort();
|
|
|
+ reject(new Error(`Timeout`));
|
|
|
+ }, 12000);//超时时间(毫秒)
|
|
|
+ }
|
|
|
+ send();
|
|
|
+ })
|
|
|
+ return promise;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export var Url = "https://meowtea2.maox.com.cn";
|
|
|
+
|
|
|
+
|
|
|
+export var NetGet = {
|
|
|
+ /**获取地图数据 */
|
|
|
+ Map: Url + "/api/tokenpass/Login/Map",
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+export var NetPost = {
|
|
|
+ /**登录 */
|
|
|
+ Login: "/api/user/third",
|
|
|
+
|
|
|
+ UpdateScore: "/api/user/UpdateScore",
|
|
|
+
|
|
|
+ ranking_list: "/api/user/ranking_list",
|
|
|
+
|
|
|
+
|
|
|
+}
|