windows - 为什么我不能从Actix文档样本中加载页面?

标签 windows rust rust-actix actix-web

我正在学习Actix框架。该文档具有the sample:

use actix_rt::System;
use actix_web::{web, App, HttpResponse, HttpServer};
use std::sync::mpsc;
use std::thread;

#[actix_rt::main]
async fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let sys = System::new("http-server");

        let srv = HttpServer::new(|| App::new().route("/", web::get().to(|| HttpResponse::Ok())))
            .bind("127.0.0.1:8088")?
            .shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
            .run();

        let _ = tx.send(srv);
        sys.run()
    });

    let srv = rx.recv().unwrap();

    // pause accepting new connections
    srv.pause().await;
    // resume accepting new connections
    srv.resume().await;
    // stop server
    srv.stop(true).await;
}

编译此代码后,我没有任何错误:

enter image description here

但是我无法在浏览器中打开页面:

enter image description here

我错过了什么,为什么页面无法在浏览器中打开?

最佳答案

本节是如何控制服务器在先前创建的线程中运行的示例。您可以优雅地暂停,恢复和停止服务器。这些行执行这三个 Action 。最后,服务器已停止。

    let srv = rx.recv().unwrap();

    // pause accepting new connections
    srv.pause().await;
    // resume accepting new connections
    srv.resume().await;
    // stop server
    srv.stop(true).await;

这使得该示例成为在代码段末尾自行关闭的服务器。使此代码段无限期运行的一个小更改是进行此更改:
    let srv = rx.recv().unwrap();

    // wait for any incoming connections
    srv.await;

我不建议这样做。还有其他示例,尤其是at the actix/examples repository,可能更适合使您开始如何构造actix服务器。

关于windows - 为什么我不能从Actix文档样本中加载页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59620854/

相关文章:

windows - 创建大于桌面的窗口(显示分辨率)

vector - 使用 Rust 循环遍历 RefCell 包装的 Vec

rust - 如何为 actix-web HttpResponse 创建流以逐 block 发送文件?

rust - 如何在 actix 中创建 application/json HTTPResponse?

rust - 如何将异步/标准库 future 转换为 futures 0.1?

windows - 如何自动检测并释放真正发生变化的DLL?

python - 如何在 Windows 上将多个 python 轮子打包成一个轮子?

windows - MFC 是否已弃用?

rust - 闭包内容器对象的生命周期推理

rust - 如何干净地获取 bash 历史记录的路径?