use bitunix::{run};
use ws::{connect, CloseCode, Sender};
use std::borrow::Borrow;
use std::f64::consts;
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use serde_json;
use std::thread;
use time;
mod websocket;
use websocket::sub_channel::SubChannel;
use std::rc::Rc;
use std::sync::{Mutex, Arc};
fn main() {
run();
if let Err(error) = connect("ws://zengwe.xyz/ws-futures/?from=contract&compress=0", |out| {
let out = Arc::new(Mutex::new(out));
// {
// let out = Arc::clone(&out);
// thread::spawn(move || loop {
// thread::sleep(Duration::from_secs(3));
// print!("send msg!\n");
// if let Err(e) = out.lock().unwrap().send(format!("{{\"ping\": {:?}}}", time::get_time().sec)) {
// println!("Error sending a periodic message: {:?}", e);
// }
// });
// }
// thread::sleep(Duration::from_secs(3));
let mark_price_channel = SubChannel::new("mark_price_btcusdt");
let send_msg = serde_json::to_string(&mark_price_channel).unwrap();
print!("{:?}", send_msg);
// // out.lock().unwrap().send(send_msg);
{
let out = Arc::clone(&out);
let temp_out = out.lock().unwrap();
temp_out.send(send_msg);
}
move |msg| {
// 处理在此连接上接收的消息
println!("Client 收到消息 '{}'. ", msg);
// out.send("'{\"pong\":1705631243}").unwrap();
// 关闭连接
// out.close(CloseCode::Normal);
return Ok(());
}
}) {
// 通知用户故障
println!("Failed to create WebSocket due to: {:?}", error);
}
}
let out = Arc::clone(&out);
let temp_out = out.lock().unwrap();
temp_out.send(send_msg);都会报错(Encountered an error: not connected)
删除这里在前面的线程中是可以循环send消息的
求大佬帮助!
这里不管怎么改调用send
1.在这里Arc::clone试过不行
2.sleep一会儿还是不行
刚开始学,rust有点难,还没入门提的问题可能有点弱鸡,望各位大佬能提点一下