feat: Basic websocket communication added

Started parrallel development of SousaCli to work over websockets to control this app
This commit is contained in:
Nickiel12 2022-12-18 18:26:13 -08:00
parent 589fa62e73
commit 2ed55b3aa0
3 changed files with 87 additions and 7 deletions

View file

@ -17,3 +17,6 @@ scan_dir = "0.3.3"
derive_more = "0.99.17" derive_more = "0.99.17"
id3 = "1.5.1" id3 = "1.5.1"
rodio = "0.16.0" rodio = "0.16.0"
tungstenite = "0.18.0"
workctl = "0.2.0"
crossbeam-channel = "0.5.6"

View file

@ -1,7 +1,12 @@
use rodio::{source::Source, Decoder, OutputStream, Sink}; use rodio::{Decoder, OutputStream, Sink};
use std::fs::File; use server_handling::UIRequest;
use std::io::BufReader; use std::io::BufReader;
use std::net::TcpStream;
use std::path::PathBuf; use std::path::PathBuf;
use std::thread::spawn;
use std::{fs::File, net::TcpListener};
use tungstenite::accept;
use tungstenite::protocol::WebSocket;
use clap::Parser; use clap::Parser;
use dirs_next; use dirs_next;
@ -10,6 +15,7 @@ use crate::db_operations::{DatabaseRequest, PartialTag};
pub mod db_operations; pub mod db_operations;
pub mod file_operations; pub mod file_operations;
pub mod server_handling;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about=None)] #[command(author, version, about, long_about=None)]
@ -90,11 +96,62 @@ fn main() {
let source = Decoder::new(file).unwrap(); let source = Decoder::new(file).unwrap();
sink.append(source); sink.append(source);
// Play the sound directly on the device // Play the sound directly on the device
std::thread::sleep(std::time::Duration::from_secs(5));
sink.pause(); sink.pause();
std::thread::sleep(std::time::Duration::from_secs(5)); std::thread::sleep(std::time::Duration::from_secs(2));
sink.play();
sink.sleep_until_end();
println!("{:?}", cli); let server = TcpListener::bind("127.0.0.1:9001").unwrap();
/*
for stream in server.incoming() {
spawn(move || {
let mut websocket = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
println!("is binary?: {:?}", msg.is_binary());
println!("msg: {:?}", msg);
}
}
});
}
*/
let mut sockets = Vec::<WebSocket<TcpStream>>::new();
loop {
if let Ok((stream, addr)) = server.accept() {
println!("New socket connected from: {}", addr);
//TODO: handle this error
sockets.push(accept(stream).unwrap());
println!("len = {}", sockets.len());
}
if sockets.len() == 0 {
std::thread::sleep(std::time::Duration::from_secs(1));
println!("sleeping");
}
for i in 0..sockets.len() {
if let Ok(mess) = sockets[i].read_message() {
println!("got a message from a socket");
if mess.is_text() {
println!("It was a text message!");
match server_handling::handle_request(mess.into_text().unwrap()) {
Err(error) => {
println!("There was an error decoding the message: {:?}", error)
}
Ok(req) => match req {
UIRequest::Play => sink.play(),
UIRequest::Pause => sink.pause(),
UIRequest::Skip(skip_direction) => todo!(),
UIRequest::GetList => todo!(),
UIRequest::SwitchTo(partia_tag) => todo!(),
UIRequest::GetStatus => todo!(),
},
}
}
}
}
}
} }

20
src/server_handling.rs Normal file
View file

@ -0,0 +1,20 @@
use crate::db_operations::PartialTag;
pub enum SkipDirection {
Forward,
Backward,
}
pub enum UIRequest {
Play,
Pause,
Skip(SkipDirection),
GetList,
SwitchTo(PartialTag),
GetStatus,
}
pub fn handle_request(socket_message: String) -> Result<UIRequest, ()> {
println!("Recieved a socket message: {}", socket_message);
Ok(UIRequest::Play)
}