1
0
Fork 0
mirror of https://github.com/icewind1991/clipboard-sync synced 2026-06-03 10:24:06 +02:00

some client cleanup

This commit is contained in:
Robin Appelman 2018-07-31 15:24:52 +02:00
commit a70dbab632

View file

@ -1,7 +1,6 @@
extern crate clipboard; extern crate clipboard;
extern crate serde; extern crate serde;
#[macro_use] #[macro_use] extern crate serde_derive;
extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
extern crate ws; extern crate ws;
extern crate env_logger; extern crate env_logger;
@ -9,7 +8,6 @@ extern crate env_logger;
use clipboard::{ClipboardContext, ClipboardProvider}; use clipboard::{ClipboardContext, ClipboardProvider};
use common::ClipboardCommand; use common::ClipboardCommand;
use std::{thread, thread::JoinHandle, time}; use std::{thread, thread::JoinHandle, time};
use std::sync::mpsc;
use ws::{connect, Message, Sender}; use ws::{connect, Message, Sender};
use std::env; use std::env;
@ -23,7 +21,7 @@ fn handle_command(command: ClipboardCommand, ctx: &mut ClipboardContext) {
let _ = ctx.set_contents(value); let _ = ctx.set_contents(value);
} }
} }
ClipboardCommand::Listen { session: _ } => {} _ => {}
} }
} }
@ -43,10 +41,7 @@ fn main() {
println!("connecting to {} on channel {}", url, session); println!("connecting to {} on channel {}", url, session);
connect(url, |out| { connect(url, |out| {
let (tx, rx) = mpsc::channel(); clipboard_thread(session.clone(), out);
clipboard_thread(session.clone(), rx);
let _ = tx.send(out);
move |msg: Message| { move |msg: Message| {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
@ -60,15 +55,13 @@ fn main() {
}).unwrap(); }).unwrap();
} }
fn clipboard_thread(session: String, rx: mpsc::Receiver<Sender>) -> JoinHandle<()> { fn clipboard_thread(session: String, out: Sender) -> JoinHandle<()> {
thread::spawn(move || { thread::spawn(move || {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
let hunderd_millis = time::Duration::from_millis(100); let hundred_millis = time::Duration::from_millis(100);
let mut old_clipboard = ctx.get_contents().unwrap_or_default(); let mut old_clipboard = ctx.get_contents().unwrap_or_default();
thread::sleep(hunderd_millis); thread::sleep(hundred_millis);
let out = rx.recv().unwrap();
// we need to do the listen after returning the closure for the websocket // we need to do the listen after returning the closure for the websocket
// thus we can't send this message in the factory // thus we can't send this message in the factory
@ -76,10 +69,10 @@ fn clipboard_thread(session: String, rx: mpsc::Receiver<Sender>) -> JoinHandle<(
session: session.clone() session: session.clone()
}); });
loop { loop {
thread::sleep(hunderd_millis); thread::sleep(hundred_millis);
let new_clipbaord = ctx.get_contents().unwrap_or_default(); let new_clipboard = ctx.get_contents().unwrap_or_default();
if new_clipbaord != old_clipboard { if new_clipboard != old_clipboard {
old_clipboard = new_clipbaord; old_clipboard = new_clipboard;
send_to_server(&out, &ClipboardCommand::Set { send_to_server(&out, &ClipboardCommand::Set {
session: session.clone(), session: session.clone(),
value: old_clipboard.clone(), value: old_clipboard.clone(),