Feat: finished mpd idle usage

This commit is contained in:
jcorporation 2018-08-15 15:06:45 +02:00
parent f69ec1820c
commit f0b577378e
4 changed files with 32 additions and 46 deletions

View File

@ -646,6 +646,7 @@ function webSocketConnect() {
showNotification('Connected to myMPD', '', '', 'success');
modalConnectionError.hide();
appRoute();
sendAPI({"cmd":"MPD_API_GET_STATE"}, parseState);
}
socket.onmessage = function got_packet(msg) {

View File

@ -37,9 +37,6 @@
#include "config.h"
#include "../dist/src/frozen/frozen.h"
/* forward declaration */
void mympd_notify(struct mg_mgr *s);
const char * mpd_cmd_strs[] = {
MPD_CMDS(GEN_STR)
};
@ -497,11 +494,15 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
free(cmd);
}
int mympd_close_handler(struct mg_connection *c) {
/* Cleanup session data */
if (c->user_data)
free(c->user_data);
return 0;
void mympd_notify(struct mg_mgr *s) {
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
if (!is_websocket(c))
continue;
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
}
#ifdef DEBUG
fprintf(stderr,"NOTIFY: %s\n", mpd.buf);
#endif
}
void mympd_poll(struct mg_mgr *s, int timeout) {
@ -547,7 +548,6 @@ void mympd_poll(struct mg_mgr *s, int timeout) {
fprintf(stderr, "MPD connection failed.\n");
snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}");
mympd_notify(s);
case MPD_DISCONNECT:
case MPD_RECONNECT:
if (mpd.conn != NULL)
@ -569,21 +569,8 @@ void mympd_poll(struct mg_mgr *s, int timeout) {
}
}
void mympd_notify(struct mg_mgr *s) {
for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) {
if (!is_websocket(c))
continue;
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
}
#ifdef DEBUG
fprintf(stderr,"NOTIFY: %s\n", mpd.buf);
#endif
}
void mympd_parse_idle(struct mg_mgr *s) {
// mpd_connection_set_timeout(mpd.conn, 60);
enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false);
// mpd_connection_set_timeout(mpd.conn, mpd.timeout);
int len;
for (unsigned j = 0;; ++j) {
@ -659,7 +646,7 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsig
mpd.conn_state = MPD_FAILURE;
return 0;
}
if (status) {
else {
audioformat = mpd_status_get_audio_format(status);
}

View File

@ -145,7 +145,7 @@ static int is_websocket(const struct mg_connection *nc) {
void mympd_poll(struct mg_mgr *sm, int timeout);
void mympd_parse_idle(struct mg_mgr *s);
void callback_mympd(struct mg_connection *nc, const struct mg_str msg);
int mympd_close_handler(struct mg_connection *c);
void mympd_notify(struct mg_mgr *s);
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version);
int mympd_put_outputs(char *buffer);
int mympd_put_current_song(char *buffer);
@ -166,4 +166,3 @@ int mympd_put_songdetails(char *buffer, char *uri);
int mympd_queue_crop(char *buffer);
void mympd_disconnect();
#endif

View File

@ -84,7 +84,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
#ifdef DEBUG
printf("Websocket connection closed\n");
#endif
mympd_close_handler(nc);
// mympd_close_handler(nc);
}
else {
#ifdef DEBUG
@ -255,10 +255,9 @@ int main(int argc, char **argv) {
if (config.ssl == true)
printf("myMPD started on ssl port %s\n", config.sslport);
//Main loop for http handling
while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 100);
mympd_poll(&mgr, 1);
mympd_poll(&mgr, 0);
}
mg_mgr_free(&mgr);
mympd_disconnect();