refactor: moved socket_message code to function
This commit is contained in:
parent
8ba2983142
commit
7cc8b81599
1 changed files with 52 additions and 63 deletions
115
src/main.rs
115
src/main.rs
|
@ -124,29 +124,21 @@ fn main() {
|
||||||
Ok(req) => match req {
|
Ok(req) => match req {
|
||||||
UIRequest::Play => {
|
UIRequest::Play => {
|
||||||
sink.play();
|
sink.play();
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"Player Paused".to_string(),
|
||||||
message: "Player Resumed".into(),
|
vec![],
|
||||||
search_results: vec![],
|
)
|
||||||
})
|
.unwrap();
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
UIRequest::Pause => {
|
UIRequest::Pause => {
|
||||||
sink.pause();
|
sink.pause();
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"Player Paused".to_string(),
|
||||||
message: "Player Paused".into(),
|
vec![],
|
||||||
search_results: vec![],
|
)
|
||||||
})
|
.unwrap();
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
UIRequest::Skip(skip_direction) => todo!(),
|
UIRequest::Skip(skip_direction) => todo!(),
|
||||||
UIRequest::Search(request) => {
|
UIRequest::Search(request) => {
|
||||||
|
@ -161,16 +153,12 @@ fn main() {
|
||||||
match items {
|
match items {
|
||||||
None => sockets[i].write_message("None".into()).unwrap(),
|
None => sockets[i].write_message("None".into()).unwrap(),
|
||||||
Some(items) => {
|
Some(items) => {
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"Here are the results:".to_string(),
|
||||||
message: "Here are the results:".to_string(),
|
items,
|
||||||
search_results: items,
|
)
|
||||||
})
|
.unwrap();
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,31 +174,21 @@ fn main() {
|
||||||
|
|
||||||
match items {
|
match items {
|
||||||
None => {
|
None => {
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"No song found with that title!".to_string(),
|
||||||
message: "No song found with that title!"
|
vec![],
|
||||||
.to_string(),
|
)
|
||||||
search_results: vec![],
|
.unwrap();
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
Some(items) => {
|
Some(items) => {
|
||||||
if items.len() > 1 {
|
if items.len() > 1 {
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"Please be more specific".to_string(),
|
||||||
message: "Please be more specific"
|
items,
|
||||||
.to_string(),
|
)
|
||||||
search_results: items,
|
.unwrap();
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"Switching song to: '{}'",
|
"Switching song to: '{}'",
|
||||||
|
@ -228,17 +206,13 @@ fn main() {
|
||||||
sink.append(source);
|
sink.append(source);
|
||||||
println!("{}", items.get(0).unwrap().path.clone());
|
println!("{}", items.get(0).unwrap().path.clone());
|
||||||
|
|
||||||
sockets[i]
|
write_to_socket(
|
||||||
.write_message(
|
&mut sockets[i],
|
||||||
serde_json::to_string(&ServerResponse {
|
"Switching now playing".to_string(),
|
||||||
message: "Switching now playing"
|
items,
|
||||||
.to_string(),
|
)
|
||||||
search_results: items,
|
.unwrap();
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
sink.play();
|
sink.play();
|
||||||
println!("{}", sink.is_paused());
|
println!("{}", sink.is_paused());
|
||||||
}
|
}
|
||||||
|
@ -253,3 +227,18 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_to_socket(
|
||||||
|
socket: &mut WebSocket<TcpStream>,
|
||||||
|
message: String,
|
||||||
|
results: Vec<message_types::ItemTag>,
|
||||||
|
) -> Result<(), tungstenite::Error> {
|
||||||
|
socket.write_message(
|
||||||
|
serde_json::to_string(&ServerResponse {
|
||||||
|
message,
|
||||||
|
search_results: results,
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue