为什么我无法从 Actix 文档示例加载页面?
Why can I not load the page from the Actix documentation sample?
我正在学习 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;
}
我编译这段代码后没有报错:
但是我无法在我的浏览器中打开该页面:
我错过了什么,为什么我的浏览器打不开页面?
本节举例说明如何在之前创建的线程中控制服务器运行ning。您可以优雅地暂停、恢复和停止服务器。这些行执行这三个操作。最后,服务器停止。
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 服务器。
我正在学习 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;
}
我编译这段代码后没有报错:
但是我无法在我的浏览器中打开该页面:
我错过了什么,为什么我的浏览器打不开页面?
本节举例说明如何在之前创建的线程中控制服务器运行ning。您可以优雅地暂停、恢复和停止服务器。这些行执行这三个操作。最后,服务器停止。
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 服务器。