首页 > 解决方案 > 在 dart http 服务器中检测客户端断开连接

问题描述

我使用 dart http 服务器作为模拟服务器来测试我的颤振应用程序与真实服务器的交互,我正在做这样的事情:

    final _server = await HttpServer.bind(InternetAddress.anyIPv4, 0);
    _server.listen(handleRequest, onError: (error) {
      print("onError");
    }, onDone: () {
      print("Closed");
    });

我的一个测试用例包括用户取消一个长时间运行的请求。我想测试请求是否真的被取消并且连接是否关闭。

所以我需要一种方法来在服务器端检测请求是否被取消:

void handleRequest(HttpRequest request) async {
    // This future is finished, when the test decides, that the client should have canceled the request
    await waitFuture;
    request.response
      ..statusCode
      ..close():
    // How do I know here if the client has canceled the request in the meantime?

不幸的是,服务器似乎只是忽略了任何客户端断开连接。

所以我的问题是:有没有一种方法可以使用飞镖 HTTP 服务器检测客户端断开连接?

标签: darthttpserver

解决方案


推荐阅读