我正在尝试将通道的mpsc::Receiver
对象赋予线程,它将仅由websocket客户端结构调用。
我已经迷惑了生命周期,弧线,互斥体,并且我对Rust的了解不足,无法使其正常工作。
感谢您提供的任何帮助。
extern crate ws;
use ws::util::Token;
use ws::{connect, Handler, Handshake, Message, Result};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::thread;
fn main() {
let (tx1, rx1) = sync_channel(100);
let (tx2, rx2) = sync_channel(100);
// set up rx2 and tx1
let client = Client::new_thread(rx1, tx2);
client.join().unwrap();
}
pub struct Client {
pub out: ws::Sender,
tx: SyncSender<Message>,
rx: Receiver<Message>,
}
impl Client {
pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
thread::spawn(move || {
connect("ws://localhost:8000/ws/server/main", |out| Client {
out,
tx: tx.clone(),
rx: rx,
}).unwrap()
})
}
}
impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.timeout(250, Token(1))
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.tx.send(msg).unwrap();
Ok(())
}
fn on_timeout(&mut self, event: Token) -> Result<()> {
//let msg = self.rx.recv()
//self.out.send(msg)
self.out.timeout(250, event)
}
}
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:31:21
|
26 | pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
| -- captured outer variable
...
31 | rx: rx,
| ^^ cannot move out of captured outer variable in an `FnMut` closure