gpt4 book ai didi

rust - 我如何在 Iron 中使用 Server-Sent 事件?

转载 作者:行者123 更新时间:2023-11-29 08:05:18 25 4
gpt4 key购买 nike

我有一个小型 Rust 应用程序,它通过串行端口接收一些请求,进行一些处理并将结果保存在本地。我想使用浏览器作为远程监视器,这样我就可以看到正在发生的一切,据我所知,SSE 非常适合这一点。

我尝试为此使用 Iron,但找不到保持连接打开的方法。请求处理程序都需要返回一个 Response,所以我不能继续发送数据。

这是我的(愚蠢的)尝试:

fn monitor(req: &mut Request) -> IronResult<Response> {
let mut headers = Headers::new();
headers.set(ContentType(Mime(TopLevel::Text, SubLevel::EventStream, vec![])));
headers.set(CacheControl(vec![CacheDirective::NoCache]));

println!("{:?}", req);

let mut count = 0;
loop {
let mut response = Response::with((iron::status::Ok, format!("data: Count!:{}", count)));
response.headers = headers.clone();
return Ok(response); //obviously won't do what I want

count += 1;
std::thread::sleep_ms(1000);
}
}

最佳答案

我认为简短的回答是:你不能。当前版本的 Iron 是建立在单一的请求-响应交互之上的。这可以在您的代码中看到,因为发送响应的唯一方法是返回它;终止处理程序线程。

Iron 中存在问题 utilize the new async support in Hyper ,它本身就是 merged relatively recently .甚至还有other people trying to use Server-Send Events in Hyper还没有成功。


如果你愿意使用 Hyper master 分支,这样的事情似乎可行。无法保证这是一个好的解决方案,或者它不会耗尽您的所有 RAM 或 CPU。它似乎在 Chrome 中工作。

extern crate hyper;

use std::time::{Duration, Instant};
use std::io::prelude::*;

use hyper::{Control, Encoder, Decoder, Next };
use hyper::server::{Server, HandlerFactory, Handler, Request, Response};
use hyper::status::StatusCode;
use hyper::header::ContentType;
use hyper::net::{HttpStream};


fn main() {
let address = "0.0.0.0:7777".parse().expect("Invalid address");
let server = Server::http(&address).expect("Invalid server");

let (_listen, server_loop) = server.handle(MyFactory).expect("Failed to handle");

println!("Starting...");
server_loop.run();
}


struct MyFactory;

impl HandlerFactory<HttpStream> for MyFactory {
type Output = MyHandler;

fn create(&mut self, ctrl: Control) -> Self::Output {
MyHandler {
control: ctrl,
}
}
}


struct MyHandler {
control: Control,
}

impl Handler<HttpStream> for MyHandler {
fn on_request(&mut self, _request: Request<HttpStream>) -> Next {
println!("A request was made");
Next::write()
}

fn on_request_readable(&mut self, _request: &mut Decoder<HttpStream>) -> Next {
println!("Request has data to read");
Next::write()
}

fn on_response(&mut self, response: &mut Response) -> Next {
println!("A response is ready to be sent");

response.set_status(StatusCode::Ok);
let mime = "text/event-stream".parse().expect("Invalid MIME");
response.headers_mut().set(ContentType(mime));

every_duration(Duration:: from_secs(1), self.control.clone());

Next::wait()
}

fn on_response_writable(&mut self, response: &mut Encoder<HttpStream>) -> Next {
println!("A response can be written");

// Waited long enough, send some data
let fake_data = r#"event: userconnect
data: {"username": "bobby", "time": "02:33:48"}"#;

println!("Writing some data");
response.write_all(fake_data.as_bytes()).expect("Failed to write");
response.write_all(b"\n\n").expect("Failed to write");

Next::wait()
}
}

use std::thread;

fn every_duration(max_elapsed: Duration, control: Control) {
let mut last_sent: Option<Instant> = None;
let mut count = 0;

thread::spawn(move || {
loop {
// Terminate after a fixed number of messages
if count >= 5 {
println!("Maximum messages sent, ending");
control.ready(Next::end()).expect("Failed to trigger end");
return;
}

// Wait a little while between messages
if let Some(last) = last_sent {
let elapsed = last.elapsed();
println!("It's been {:?} since the last message", elapsed);

if elapsed < max_elapsed {
let remaining = max_elapsed - elapsed;
println!("There's {:?} remaining", remaining);
thread::sleep(remaining);
}
}

// Trigger a message
control.ready(Next::write()).expect("Failed to trigger write");

last_sent = Some(Instant::now());
count += 1;
}
});
}

和客户端JS:

var evtSource = new EventSource("http://127.0.0.1:7777");

evtSource.addEventListener("userconnect", function(e) {
const obj = JSON.parse(e.data);
console.log(obj);
}, false);

关于rust - 我如何在 Iron 中使用 Server-Sent 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com