瀏覽代碼

[通信][修复]:访问增加超时机制

tags/jenkins-front-end-screenad-139-v89^2
hupeng 6 年之前
父節點
當前提交
a5f81efe3d
共有 2 個檔案被更改,包括 65 行新增20 行删除
  1. +31
    -13
      App.js
  2. +34
    -7
      HttpUtils.js

+ 31
- 13
App.js 查看文件

@@ -89,6 +89,9 @@ var heartConfig = {
pageChangeInterval:10,
}

var requestFailedCount = 0;
var versionInfo = '';

export default class MyPage extends Component {
constructor(props) {
super(props);
@@ -122,8 +125,10 @@ codePushUpdate() {
CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
.then((metadata: LocalPackage) => {
this.setState({ versionInfo: metadata ? metadata.appVersion +'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : "Unknown"});
versionInfo = this.state.versionInfo;
}, (error: any) => {
this.setState({ versionInfo: "Unknown"});
versionInfo = this.state.versionInfo;
});
}

@@ -186,9 +191,10 @@ codePushStatusDidChange(syncStatus) {
}
hbRequest() {
return HttpUtils.post(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/heartbeat?deviceId='
+encodeURIComponent(this.state.macAddress),
{version:this.state.versionInfo})
return HttpUtils.post(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/heartbeat?deviceId='
+ encodeURIComponent(this.state.macAddress),
{version:this.state.versionInfo})
.then(result => {
if (result.code == 200) {
let config = JSON.parse(result.data.config);
@@ -223,7 +229,8 @@ codePushStatusDidChange(syncStatus) {
})
.catch(error => {
this.setState({ errorMessage : error.message});
requestFailedCount++
this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`});
})
}

@@ -232,9 +239,12 @@ codePushStatusDidChange(syncStatus) {
this.setState({errorMessage : errMsg});
}

getAdDataList() {
return HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/list?deviceId='+encodeURIComponent(this.state.macAddress)+'&pageNum=1&pageSize=1000')
.then((result) => {
getAdDataList(first) {
HttpUtils.get(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/list?deviceId='
+ encodeURIComponent(this.state.macAddress)
+ '&pageNum=1&pageSize=1000')
.then(result => {
if (result.code != 200) {
this.showErrMsg(result.message+'\n'+this.state.macAddress);
} else if (result.data.list) {
@@ -249,11 +259,19 @@ codePushStatusDidChange(syncStatus) {
}
})
.catch(error => {
this.showErrMsg(error.message);
if (first) {
this.showErrMsg(error.message);
}
else {
requestFailedCount++
this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`});
}
})
}
getBaseInfo() {
return HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/info?deviceId='+encodeURIComponent(this.state.macAddress))
HttpUtils.get(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/info?deviceId='
+ encodeURIComponent(this.state.macAddress))
.then(result => {
if (result.code != 200) {
this.showErrMsg(result.message+'\n'+this.state.macAddress);
@@ -296,7 +314,7 @@ async routine() {

try {
if (getAdDataCount<=0) {
await this.getAdDataList()
await this.getAdDataList(false)
getAdDataCount = heartConfig.dataRefreshInterval
} else if (hbRequestCount<=0) {
await this.hbRequest()
@@ -311,12 +329,12 @@ async routine() {
}
} else {
await this.getBaseInfo()
await this.getAdDataList()
await this.getAdDataList(true)
}
pageChangeCount = heartConfig.pageChangeInterval
}
} catch(e){
this.setState({ versionInfo: this.state.versionInfo+'[E]'});
this.setState({ versionInfo: versionInfo+'[E]'});
}

setTimeout(()=>{
@@ -326,7 +344,7 @@ async routine() {

async startAd() {
await this.getBaseInfo()
await this.getAdDataList()
await this.getAdDataList(true)
await this.hbRequest()
hbRequestCount = heartConfig.heartbeatInterval;


+ 34
- 7
HttpUtils.js 查看文件

@@ -1,16 +1,30 @@
export default class HttpUtil {
/**
* 利用 Promise 的 get 方式请求
* @param url
* @returns {Promise}
*/
static get(url) {
return new Promise((resolve, reject) => {
request = new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(result => resolve(result))
})
}).catch(e => reject(e.message));
const timeoutRequest = new Promise((resolve, reject) => {
setTimeout(reject, 30000, 'Request timeout');
});
return Promise
.race([request, timeoutRequest])
.then(res => {
return res
}, m => {
throw new Error(m);
});
}
/**
@@ -20,7 +34,7 @@ export default class HttpUtil {
* @returns {Promise}
*/
static post(url, params) {
return new Promise((resolve, reject) => {
request = new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: {
@@ -28,10 +42,23 @@ export default class HttpUtil {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
})
.then(response => response.json())
.then(result => resolve(result))
})
}).then(response => response.json())
.then(result => resolve(result))
}).catch(e => reject(e.message));
const timeoutRequest = new Promise((resolve, reject) => {
setTimeout(reject, 30000, 'Request timeout');
});
return Promise
.race([request, timeoutRequest])
.then(res => {
return res
}, m => {
throw new Error(m);
});
}
}

Loading…
取消
儲存