mirror of
https://github.com/icewind1991/clipboard-sync
synced 2026-06-03 18:34:07 +02:00
some server code cleanup
This commit is contained in:
parent
e4c6d2d048
commit
2bfe6381cb
1 changed files with 8 additions and 20 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate mio;
|
extern crate mio;
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -15,31 +14,20 @@ use ws::{CloseCode, Error, Handler, listen, Message, Result, Sender};
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
struct Session {
|
struct Session {
|
||||||
clients: HashMap<Token, Sender>,
|
clients: HashMap<Token, Sender>
|
||||||
value: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_command(command: ClipboardCommand, sessions: &mut HashMap<String, Session>, client: Sender) {
|
fn handle_command(command: ClipboardCommand, sessions: &mut HashMap<String, Session>, client: Sender) {
|
||||||
match command {
|
match command {
|
||||||
ClipboardCommand::Listen { session: session_name } => {
|
ClipboardCommand::Listen { session: session_name } => {
|
||||||
if !sessions.contains_key(&session_name) {
|
sessions.entry(session_name).or_insert_with(|| Session {
|
||||||
let mut new_session = Session {
|
clients: HashMap::new()
|
||||||
clients: HashMap::new(),
|
}).clients.insert(client.token(), client);
|
||||||
value: String::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
new_session.clients.insert(client.token(), client);
|
|
||||||
sessions.insert(session_name.clone(), new_session);
|
|
||||||
} else {
|
|
||||||
let session = sessions.get_mut(&session_name).unwrap();
|
|
||||||
session.clients.insert(client.token(), client);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ClipboardCommand::Set { value, session: session_name } => {
|
ClipboardCommand::Set { value, session: session_name } => {
|
||||||
match sessions.get_mut(&session_name) {
|
match sessions.get_mut(&session_name) {
|
||||||
Some(mut session) => {
|
Some(mut session) => {
|
||||||
session.value = value.clone();
|
|
||||||
send_to_session(session, &ClipboardCommand::Set {
|
send_to_session(session, &ClipboardCommand::Set {
|
||||||
value,
|
value,
|
||||||
session: session_name,
|
session: session_name,
|
||||||
|
|
@ -97,13 +85,13 @@ impl Handler for Server {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let port = std::env::var("PORT").unwrap_or("80".to_string());
|
let port = std::env::var("PORT").unwrap_or("80".to_string());
|
||||||
let listen_adress = format!("0.0.0.0:{}", port);
|
let listen_address = format!("0.0.0.0:{}", port);
|
||||||
|
|
||||||
println!("listening on: {:?}", listen_adress);
|
println!("listening on: {:?}", listen_address);
|
||||||
|
|
||||||
let sessions: Rc<RefCell<HashMap<String, Session>>> = Rc::new(RefCell::new(HashMap::new()));
|
let sessions: Rc<RefCell<HashMap<String, Session>>> = Rc::new(RefCell::new(HashMap::new()));
|
||||||
|
|
||||||
let result = listen(listen_adress, |out| { Server { out, sessions: sessions.clone() } });
|
let result = listen(listen_address, |out| { Server { out, sessions: sessions.clone() } });
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue