我正在学习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;
}
编译此代码后,我没有任何错误:
但是我无法在浏览器中打开页面:
我错过了什么,为什么页面无法在浏览器中打开?
最佳答案
本节是如何控制服务器在先前创建的线程中运行的示例。您可以优雅地暂停,恢复和停止服务器。这些行执行这三个 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/