如何解决Rust Actix Actor 向 actor 发送消息
如何向另一个actor发送消息?
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int size = 10;
var grid = GetExplosionTemplate(size,new Random(),1);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Console.Write(grid[i + j * size] ? "X" : "O");
}
Console.WriteLine();
}
}
private static bool[] GetExplosionTemplate(int size,Random rnd = null,float explosionRoughness = 0)
{
var grid = new bool[size * size];
var rr = (size + 1) / 2f;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
var cellId = i + j * size;
var r = Math.Sqrt(Math.Pow(i - size / 2,2) + Math.Pow(j - size / 2,2)) + rnd?.NextDouble()*explosionRoughness ?? 0;
grid[cellId] = r > rr;
}
}
return grid;
}
}
}
我可以制作一个演员的 hashMap 吗?
pub struct MyWs {
}
impl Actor for MyWs {
type Context = ws::WebsocketContext<Self>;
}
impl StreamHandler<Result<ws::Message,ws::ProtocolError>> for MyWs {
fn handle(&mut self,msg: Result<ws::Message,ws::ProtocolError>,ctx: &mut Self::Context) {
match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),Ok(ws::Message::Text(message)) => {
//considering that here he sent the message to self
ctx.text(message);
//how to do something like this
//find the actor by index (or uuid) and send text
//actors[0].text(message);
//
},Ok(ws::Message::Binary(bin)) => ctx.binary(bin),Ok(ws::Message::Close(reason)) => ctx.close(reason),_ => (),}
}
}
#[get("/ws")]
pub async fn websocket(req: HttpRequest,stream: web::Payload,) -> actix_web::Result<HttpResponse> {
let resp = ws::start(
MyWs {},&req,stream,);
return resp;
}
以后
pub struct MyWs { sessions: HashMap<Uuid,Socket> }
我是 Rust 的新手,我没有看到保存套接字(上下文或参与者)以找到它并向其发送消息的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。