【问题标题】:Why can I not load the page from the Actix documentation sample?为什么我无法从 Actix 文档示例加载页面?
【发布时间】:2020-01-07 13:27:16
【问题描述】:

我正在学习 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;
}

编译这段代码后没有错误:

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

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

【问题讨论】:

  • // stop server srv.stop(true).await; 你要停止服务器吗?
  • 没有。我该怎么办?我需要它吗?
  • srv.pause / srv.resume / srv.stop 的行控制着你的服务器是否正在运行。如果您想让它无限期运行,请尝试将这些行替换为 srv.await

标签: windows rust rust-actix actix-web


【解决方案1】:

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

    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;

这使得这个示例成为一个在 sn-p 结束时自行关闭的服务器。让这个 sn-p 无限期运行的一个小改动是进行此更改:

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

    // wait for any incoming connections
    srv.await;

这不是我推荐的。还有其他示例,尤其是 at the actix/examples repository,可能更适合帮助您开始了解如何构建 actix 服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2021-05-14
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多