java - web端百度网盘的一个操作为什么要分两次请求服务器, 有什么好处吗
迷茫
迷茫 2017-04-18 10:48:35
0
3
843

以 文件重命名 为例:

  • 当完成重命名操作提交会到这个地址
    https://pan.baidu.com/api/filemanager

返回如下结果

{
  "errno": 0,
  "info": [],
  "request_id": 88137407060055336,
  "taskid": 307843054247316
}

可以联想到在server端建立了一个task, 并返回了taskid让客户端后续取状态来更新ui

  • 客户端轮训服务器的接口 https://pan.baidu.com/share/taskquery 来获取状态, 1秒一次请求, 服务器端返回结果如下: 分几种情况我总结了一下

#进行中的返回值
{
  "errno": 0,
  "request_id": 88137707954758994,
  "task_errno": 0,
  "status": "pending"
}

#进行中
{
  "errno": 0,
  "request_id": 88137707954758994,
  "task_errno": 0,
  "status": "running"
}


#操作成功的返回值
{
  "errno": 0,
  "request_id": 88138584419582326,
  "task_errno": 0,
  "status": "success",
  "list": [
    {
      "from": "/test1/我的照片",
      "to": "/test1/我的照片2"
    }
  ],
  "total": 1
}

当 status 为success时候, 则轮询结束, 更新UI元素

问题: 直接访问重命名接口不行吗? 为什么要这么设计, 好处是什么?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
伊谢尔伦

You have made it very clear. Is there anything else you don’t understand?

The first time is to initiate a name change application to the server.
The server will start the task.
The subsequent polling is to check whether the task is completed, and the front-end will perform corresponding operations after completion. In case of failure, the front-end will also perform rollback operations.

Ty80

Guess the reason: In extreme cases, the operation may take a long time and cannot be returned immediately. When the operation is completed, the socket link may have been disconnected and the final result cannot be obtained. Designed as a task queue, customers can be guaranteed Get the final result.

PHPzhong

In addition to the above considerations, there may be another very important reason, and that is concurrency pressure. Made asynchronous, it can solve concurrency very well

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!