我正在学习Rust并试图编写websocket服务器。逻辑如下:WSConnectionFactory创建处理传入消息并根据任意规则将其发送到其他客户端的WSHandler。 问题是我不知道如何实现这样的行为。如何在散列表字段中保存对处理程序的引用
限制:我不能更改Factory和Handler特征的签名,因为它们由ws-rs库提供。
问:如何使用RefCell/Cell实现这个功能?
extern crate rand;
extern crate rustc_serialize;
extern crate ws;
#[macro_use]
extern crate log;
#[macro_use]
extern crate env_logger;
use std::cell::RefCell;
use std::collections::HashMap;
use rand::random;
use ws::{Factory, Sender, Handler, Handshake, Message, CloseCode, WebSocket};
use ws::Result as WSResult;
use ws::util::Token;
struct WSConnectionFactory<'p> {
handlers: HashMap<&'p u32, RefCell<&'p WSHandler<'p>>>,
}
#[derive(Debug)]
struct WSHandler<'h> {
uid: &'h u32,
ws: RefCell<&'h Sender>,
}
impl<'p> Factory for WSConnectionFactory<'p> {
type Handler = WSHandler<'p>;
fn connection_made(&mut self, ws: Sender) -> Self::Handler {
println!("factory.connection_made token={:?}", &ws.token());
let uid = &random::<u32>();
let handler = WSHandler {
uid: uid,
ws: RefCell::new(&ws),
};
self.handlers.insert(uid, RefCell::new(&handler));
handler
}
}
impl<'h> Handler for WSHandler<'h> {
fn on_open(&mut self, _handshake: Handshake) -> WSResult<()> {
println!("handler.on_open");
Ok(())
}
fn on_message(&mut self, msg: Message) -> WSResult<()> {
println!("handler.on_message {:?}", msg);
Ok(())
}
fn on_timeout(&mut self, _token: Token) -> WSResult<()> {
println!("handler.on_timeout {:?}", _token);
Ok(())
}
fn on_close(&mut self, code: CloseCode, reason: &str) {
println!("handler.on_close code={:?}, reason={:?}", code, reason);
}
}
fn main() {
let factory = WSConnectionFactory { handlers: HashMap::new() };
let ws_socket = WebSocket::new(factory).expect("Can't create WebSocket");
ws_socket.listen("127.0.0.1:8080").expect("Can't bind to socket");
}
感谢您的回答,我错过了克隆是为发件人实施的。 – Sergey