requestTask.uvue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view style="flex: 1;">
  3. <view class="uni-padding-wrap uni-common-mt">
  4. <view class="uni-common-mt" style="border-width: 2px;border-style: solid; border-radius: 4px;">
  5. <textarea :value="res" class="uni-textarea" style="width: 100%"></textarea>
  6. </view>
  7. <view>
  8. <text>地址 : {{ host + url}}</text>
  9. <text>请求方式 : {{method}}</text>
  10. </view>
  11. <view class="uni-btn-v uni-common-mt">
  12. <button type="primary" @tap="checkRequestTask" id="checkRequestTask">发起流式请求(设置监听需重新点击、勿重复点击)</button>
  13. <view class="uni-common-pb"></view>
  14. <button type="primary" @tap="abort" id="abort">中断流式请求</button>
  15. </view>
  16. </view>
  17. <scroll-view style="flex: 1;" show-scrollbar="true">
  18. <view style="padding: 20px;">
  19. <text>添加或者移除流式监听</text>
  20. <view class="uni-common-pb"></view>
  21. <view style="flex-direction: row;flex-wrap: wrap;">
  22. <button style="padding: 5px;" type="primary" size="mini" @tap="onHeadersReceived_observe_1"
  23. id="onHeadersReceived_observe_1">onHeadersReceived监听1</button>
  24. <button style="padding: 5px;" type="primary" size="mini" @tap="onHeadersReceived_observe_2"
  25. id="onHeadersReceived_observe_2">onHeadersReceived监听2</button>
  26. <button style="padding: 5px;" type="primary" size="mini" @tap="onChunkReceived_observe_1"
  27. id="onChunkReceived_observe_1">onChunkReceived监听1</button>
  28. <button style="padding: 5px;" type="primary" size="mini" @tap="onChunkReceived_observe_2"
  29. id="onChunkReceived_observe_2">onChunkReceived监听2</button>
  30. <button style="padding: 5px;" type="primary" size="mini" @tap="offHeadersReceived_id"
  31. id="offHeadersReceived_id">offHeadersReceived(id)</button>
  32. <button style="padding: 5px;" type="primary" size="mini" @tap="offHeadersReceived_observe"
  33. id="offHeadersReceived_observe">offHeadersReceived移除所有</button>
  34. <button style="padding: 5px;" type="primary" size="mini" @tap="offChunkReceived_observe"
  35. id="offChunkReceived_observe">offChunkReceived移除所有</button>
  36. </view>
  37. </view>
  38. </scroll-view>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. res: '',
  46. task: null as RequestTask | null,
  47. host: "https://request.dcloud.net.cn",
  48. url: "/api/http/contentType/eventStream?limit=10",
  49. method: "POST" as RequestMethod,
  50. data: null as any | null,
  51. onHeadersReceivedObseves: [] as number[],
  52. onChunkReceivedObseves: [] as number[],
  53. onHeadersReceived_returnid_1: -1,
  54. onHeadersReceived_returnid_2: -1,
  55. isAbort: false,
  56. }
  57. },
  58. onLoad() {
  59. this.onHeadersReceivedObseves.push(1)
  60. this.onChunkReceivedObseves.push(1)
  61. },
  62. onUnload() {
  63. uni.hideLoading();
  64. this.task?.abort();
  65. },
  66. methods: {
  67. onHeadersReceived_observe_1: function () {
  68. this.onHeadersReceivedObseves.push(1)
  69. },
  70. onHeadersReceived_observe_2: function () {
  71. this.onHeadersReceivedObseves.push(2)
  72. },
  73. offHeadersReceived_observe: function () {
  74. this.onHeadersReceivedObseves = []
  75. },
  76. offHeadersReceived_id: function () {
  77. this.onHeadersReceivedObseves = []
  78. this.onHeadersReceivedObseves.push(1)
  79. },
  80. onChunkReceived_observe_1: function () {
  81. this.onChunkReceivedObseves.push(1)
  82. },
  83. onChunkReceived_observe_2: function () {
  84. this.onChunkReceivedObseves.push(2)
  85. },
  86. offChunkReceived_observe: function () {
  87. this.onChunkReceivedObseves = []
  88. },
  89. checkRequestTask: function () {
  90. this.isAbort = false
  91. this.res = '发起post流式请求 \n\n'
  92. this.task = uni.request({
  93. url: "https://request.dcloud.net.cn/api/http/contentType/eventStream?limit=10",
  94. timeout: 600000,
  95. method: this.method,
  96. enableChunked: true,
  97. success: (res) => {
  98. console.log('request success', JSON.stringify(res.data))
  99. console.log('request success header is :', JSON.stringify(res.header))
  100. uni.showToast({
  101. title: '流式请求结束',
  102. icon: 'success',
  103. mask: true
  104. });
  105. console.log('请求结果 : ' + JSON.stringify(res))
  106. },
  107. fail: (err) => {
  108. let content = err.errMsg
  109. if (this.isAbort) {
  110. content = "中断成功"
  111. }
  112. console.log('request fail', err);
  113. uni.showModal({
  114. content: content,
  115. showCancel: false
  116. });
  117. },
  118. complete: () => {
  119. this.task = null
  120. },
  121. });
  122. const onHeadersReceivedCallback1 = (res : RequestTaskOnHeadersReceivedListenerResult) => {
  123. console.log("-------onHeadersReceived监听1------", res)
  124. this.res += `onHeadersReceived监听1:\n ${JSON.stringify(res)} \n\n`
  125. }
  126. const onHeadersReceivedCallback2 = (res : RequestTaskOnHeadersReceivedListenerResult) => {
  127. console.log("-------onHeadersReceived监听2------", res)
  128. this.res += `onHeadersReceived监听2:\n ${JSON.stringify(res)} \n\n`
  129. }
  130. if (this.onHeadersReceivedObseves.includes(1)) {
  131. this.onHeadersReceived_returnid_1 = this.task?.onHeadersReceived(onHeadersReceivedCallback1) ?? -1
  132. }
  133. if (this.onHeadersReceivedObseves.length == 0) {
  134. this.task?.offHeadersReceived(null)
  135. this.res += "点击了 offHeadersReceived \n\n"
  136. } else if (this.onHeadersReceivedObseves.length == 1) {
  137. this.task?.offHeadersReceived(this.onHeadersReceived_returnid_2)
  138. }
  139. const onChunkReceivedCallback1 = (res : RequestTaskOnChunkReceivedListenerResult) => {
  140. const chunkText : string = new TextDecoder().decode(res.data)
  141. console.log("-------onChunkReceived监听1------", chunkText)
  142. this.res += `onChunkReceived监听1:\n ${chunkText}`
  143. }
  144. const onChunkReceivedCallback2 = (res : RequestTaskOnChunkReceivedListenerResult) => {
  145. const chunkText : string = new TextDecoder().decode(res.data)
  146. console.log("-------onChunkReceived监听2------", chunkText)
  147. this.res += `onChunkReceived监听2:\n ${chunkText}`
  148. }
  149. if (this.onHeadersReceivedObseves.includes(2)) {
  150. this.onHeadersReceived_returnid_2 = this.task?.onHeadersReceived(onHeadersReceivedCallback2) ?? -1
  151. }
  152. if (this.onChunkReceivedObseves.includes(1)) {
  153. this.task?.onChunkReceived(onChunkReceivedCallback1)
  154. }
  155. if (this.onChunkReceivedObseves.includes(2)) {
  156. this.task?.onChunkReceived(onChunkReceivedCallback2)
  157. }
  158. if (this.onChunkReceivedObseves.length == 0) {
  159. this.task?.offChunkReceived(null)
  160. this.res += "点击了 offChunkReceived \n\n"
  161. }
  162. },
  163. abort() {
  164. this.isAbort = true
  165. this.task?.abort()
  166. }
  167. }
  168. }
  169. </script>
  170. <style>
  171. .uni-textarea {
  172. padding: 9px;
  173. font-size: 14px;
  174. }
  175. </style>