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

时间:2016-07-04 07:31:00

标签: rust iron

我有一个小的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);
    }
}

1 个答案:

答案 0 :(得分:4)

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

Iron utilize the new async support in Hyper中存在一个问题,merged relatively recently本身就是other people trying to use Server-Send Events in Hyper。甚至还有{{3}}尚未成功。

如果您愿意使用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);