Hello.
I'm having trouble sending messages using actors address.
how to fix it?
Code:
//My actor
pub struct DataHub{}
impl Actor for DataHub
{
type Context = Context<Self>;
fn started(&mut self, _ctx: &mut Context<Self>) { }
fn stopped(&mut self, _ctx: &mut Context<Self>) { }
}
impl Handler<MyMsg> for DataHub
{
type Result = Result<String, Error>;
fn handle(&mut self, msg: MyMsg, _: &mut Self::Context) -> Self::Result
{
//..any code
}
}
// I create a my actor
let dh_addr = DataHub::create(move |ctx: &mut Context<DataHub>| {
//..any code
});
let web_address_cloned = web::Data::new( Mutex::new(dh_addr));
async
fn index(dh_address: web::Data<Mutex<Addr<DataHub>>>, req: HttpRequest) -> HttpResponse
{
println!("{:?}", req);
// ERROR - cannot compile
let addr = dh_address.lock().unwrap();
addr.send(MyMsg::new());
HttpResponse::Ok().finish()
}
HttpServer::new(move || {
App::new().app_data(web_address_cloned.clone())
.service(web::resource("/").to(index))
}).bind("127.0.0.1:8080").unwrap().run();
let _ = system.run();
Error:
`(dyn actix_web::ResponseError + 'static)` cannot be sent between threads safely