mirror of
https://github.com/SuperBFG7/ympd
synced 2025-03-04 02:28:19 +00:00
Fixed websocket connection - Send on connect settings and output
This commit is contained in:
parent
401bd5302f
commit
998d675b2c
@ -283,10 +283,8 @@ function webSocketConnect() {
|
||||
try {
|
||||
socket.onopen = function() {
|
||||
console.log("connected");
|
||||
/* emit request for mympd settings */
|
||||
socket.send('MPD_API_GET_SETTINGS');
|
||||
/* emit initial request for output names */
|
||||
socket.send('MPD_API_GET_OUTPUTS');
|
||||
//socket.send('MPD_API_GET_OUTPUTS');
|
||||
//socket.send('MPD_API_GET_SETTINGS');
|
||||
showNotification('Connected to myMPD','','','success');
|
||||
$('#modalConnectionError').modal('hide');
|
||||
app.route();
|
||||
|
@ -4682,7 +4682,7 @@ int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
|
||||
#ifdef __GNUC__
|
||||
__attribute__((deprecated));
|
||||
#endif
|
||||
;
|
||||
|
||||
|
||||
/*
|
||||
* Gets and parses the Authorization: Basic header
|
||||
|
@ -59,7 +59,7 @@ static inline enum mpd_cmd_ids get_cmd_id(const char *cmd)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void callback_mpd(struct mg_connection *c,const struct mg_str msg)
|
||||
void callback_mpd(struct mg_connection *nc, const struct mg_str msg)
|
||||
{
|
||||
enum mpd_cmd_ids cmd_id = get_cmd_id(msg.p);
|
||||
size_t n = 0;
|
||||
@ -69,12 +69,11 @@ void callback_mpd(struct mg_connection *c,const struct mg_str msg)
|
||||
char *p_charbuf = NULL, *token;
|
||||
char *p_charbuf2 = NULL;
|
||||
char *searchstr = NULL;
|
||||
char request[500];
|
||||
|
||||
fprintf(stdout,"Got request: %s\n",msg.p);
|
||||
|
||||
if(cmd_id == -1)
|
||||
return 0;
|
||||
return;
|
||||
|
||||
switch(cmd_id)
|
||||
{
|
||||
@ -149,7 +148,7 @@ void callback_mpd(struct mg_connection *c,const struct mg_str msg)
|
||||
break;
|
||||
case MPD_API_GET_OUTPUTS:
|
||||
mpd.buf_size = mpd_put_outputs(mpd.buf, 1);
|
||||
mpd_notify_callback(c, NULL);
|
||||
mpd_notify_callback(nc, NULL);
|
||||
break;
|
||||
case MPD_API_TOGGLE_OUTPUT:
|
||||
if (sscanf(msg.p, "MPD_API_TOGGLE_OUTPUT,%u,%u", &uint_buf, &uint_buf_2)) {
|
||||
@ -467,6 +466,7 @@ out_set_replaygain:
|
||||
|
||||
if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
|
||||
{
|
||||
fprintf(stdout,"Error: %s\n",mpd_connection_get_error_message(mpd.conn));
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
|
||||
mpd_connection_get_error_message(mpd.conn));
|
||||
|
||||
@ -476,11 +476,9 @@ out_set_replaygain:
|
||||
}
|
||||
|
||||
if(n > 0) {
|
||||
//fprintf(stdout,"Send response:\n %s\n",mpd.buf);
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
fprintf(stdout,"Send response:\n %s\n",mpd.buf);
|
||||
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mpd_close_handler(struct mg_connection *c)
|
||||
@ -493,16 +491,15 @@ int mpd_close_handler(struct mg_connection *c)
|
||||
|
||||
static int mpd_notify_callback(struct mg_connection *c, const char *param) {
|
||||
size_t n;
|
||||
|
||||
if(is_websocket(c))
|
||||
if(!is_websocket(c))
|
||||
return 0;
|
||||
|
||||
if(param)
|
||||
{
|
||||
/* error message? */
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\",\"data\":\"%s\"}",param);
|
||||
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\",\"data\":\"%s\"}",param);
|
||||
fprintf(stdout,"Error in mpd_notify_callback: %s\n",param);
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -512,24 +509,28 @@ static int mpd_notify_callback(struct mg_connection *c, const char *param) {
|
||||
struct t_mpd_client_session *s = (struct t_mpd_client_session *)c->user_data;
|
||||
|
||||
if(mpd.conn_state != MPD_CONNECTED) {
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"disconnected\"}");
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"disconnected\"}");
|
||||
fprintf(stdout,"Notify: disconnected\n");
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
fprintf(stdout,"Notify: %s\n",mpd.buf);
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, mpd.buf_size);
|
||||
|
||||
if(s->song_id != mpd.song_id)
|
||||
{
|
||||
n = mpd_put_current_song(mpd.buf);
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
n=mpd_put_current_song(mpd.buf);
|
||||
fprintf(stdout,"Notify: %s\n",mpd.buf);
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
s->song_id = mpd.song_id;
|
||||
}
|
||||
|
||||
if(s->queue_version != mpd.queue_version)
|
||||
{
|
||||
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"update_queue\"}");
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
|
||||
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"update_queue\"}");
|
||||
fprintf(stdout,"Notify: update_queue\n");
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
|
||||
s->queue_version = mpd.queue_version;
|
||||
}
|
||||
|
||||
|
@ -122,7 +122,7 @@ char coverimage[40];
|
||||
|
||||
static int is_websocket(const struct mg_connection *nc) {
|
||||
return nc->flags & MG_F_IS_WEBSOCKET;
|
||||
};
|
||||
}
|
||||
|
||||
struct t_mpd_client_session {
|
||||
int song_id;
|
||||
@ -131,7 +131,7 @@ struct t_mpd_client_session {
|
||||
};
|
||||
|
||||
void mpd_poll(struct mg_mgr *s);
|
||||
void callback_mpd(struct mg_connection *c, const struct mg_str msg);
|
||||
void callback_mpd(struct mg_connection *nc, const struct mg_str msg);
|
||||
int mpd_close_handler(struct mg_connection *c);
|
||||
int mpd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version);
|
||||
int mpd_put_outputs(char *buffer, int putnames);
|
||||
|
28
src/mympd.c
28
src/mympd.c
@ -45,23 +45,39 @@ static void signal_handler(int sig_num) {
|
||||
|
||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
switch(ev) {
|
||||
case MG_EV_CLOSE: {
|
||||
if (is_websocket(nc)) {
|
||||
mpd_close_handler(nc);
|
||||
}
|
||||
break;
|
||||
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
||||
fprintf(stdout,"New Websocket connection\n");
|
||||
struct mg_str d = {(char *) "MPD_API_GET_SETTINGS", 20 };
|
||||
callback_mpd(nc, d);
|
||||
d.p="MPD_API_GET_OUTPUTS";
|
||||
d.len=19;
|
||||
callback_mpd(nc, d);
|
||||
break;
|
||||
}
|
||||
case MG_EV_WEBSOCKET_FRAME: {
|
||||
struct websocket_message *wm = (struct websocket_message *) ev_data;
|
||||
wm->data[wm->size]='\0';
|
||||
struct mg_str d = {(char *) wm->data, wm->size};
|
||||
fprintf(stdout,"Websocket request: %s\n",wm->data);
|
||||
callback_mpd(nc, d);
|
||||
break;
|
||||
}
|
||||
case MG_EV_HTTP_REQUEST: {
|
||||
mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts);
|
||||
struct http_message *hm = (struct http_message *) ev_data;
|
||||
printf("HTTP request: %.*s\n",hm->uri.len,hm->uri.p);
|
||||
mg_serve_http(nc, hm, s_http_server_opts);
|
||||
break;
|
||||
}
|
||||
case MG_EV_CLOSE: {
|
||||
if (is_websocket(nc)) {
|
||||
fprintf(stdout,"Websocket connection closed\n");
|
||||
mpd_close_handler(nc);
|
||||
}
|
||||
else {
|
||||
fprintf(stdout,"HTTP Close\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user