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
91
src/main.rs
91
src/main.rs
|
@ -124,27 +124,19 @@ fn main() {
|
|||
Ok(req) => match req {
|
||||
UIRequest::Play => {
|
||||
sink.play();
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "Player Resumed".into(),
|
||||
search_results: vec![],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"Player Paused".to_string(),
|
||||
vec![],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
UIRequest::Pause => {
|
||||
sink.pause();
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "Player Paused".into(),
|
||||
search_results: vec![],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"Player Paused".to_string(),
|
||||
vec![],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -161,14 +153,10 @@ fn main() {
|
|||
match items {
|
||||
None => sockets[i].write_message("None".into()).unwrap(),
|
||||
Some(items) => {
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "Here are the results:".to_string(),
|
||||
search_results: items,
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"Here are the results:".to_string(),
|
||||
items,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -186,29 +174,19 @@ fn main() {
|
|||
|
||||
match items {
|
||||
None => {
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "No song found with that title!"
|
||||
.to_string(),
|
||||
search_results: vec![],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"No song found with that title!".to_string(),
|
||||
vec![],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
Some(items) => {
|
||||
if items.len() > 1 {
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "Please be more specific"
|
||||
.to_string(),
|
||||
search_results: items,
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"Please be more specific".to_string(),
|
||||
items,
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
|
@ -228,17 +206,13 @@ fn main() {
|
|||
sink.append(source);
|
||||
println!("{}", items.get(0).unwrap().path.clone());
|
||||
|
||||
sockets[i]
|
||||
.write_message(
|
||||
serde_json::to_string(&ServerResponse {
|
||||
message: "Switching now playing"
|
||||
.to_string(),
|
||||
search_results: items,
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
write_to_socket(
|
||||
&mut sockets[i],
|
||||
"Switching now playing".to_string(),
|
||||
items,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
sink.play();
|
||||
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