1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00

Send queue updates back to the browser.

This commit is contained in:
Dan Barber 2013-12-06 15:48:17 +00:00
parent 4181130ce1
commit 9788390139
4 changed files with 37 additions and 14 deletions

View File

@ -11,7 +11,7 @@ $(document).ready(function() {
status: status status: status
}), }),
events: new EventSource('/api/stream'), events: new Events(queueSongs, status),
queue: new Queue('#queue', { queue: new Queue('#queue', {
queueSongs: queueSongs, queueSongs: queueSongs,
@ -19,17 +19,6 @@ $(document).ready(function() {
}) })
}; };
mpdClient.events.onmessage = function(e) {
newStatus = JSON.parse(e.data);
status.attr(newStatus);
}
status.bind('change', function(event, attr, how, newVal, oldVal) {
if (attr == 'song') {
mpdClient.queueSongs.updatePlaying(oldVal, newVal);
}
});
}); });
}); });

View File

@ -0,0 +1,27 @@
var Events = can.Construct.extend({
init: function(queue, status) {
this.events = new EventSource('/api/stream')
self = this
this.events.onmessage = function(e) {
response = JSON.parse(e.data);
switch (response.type) {
case 'status':
status.attr(response.data);
break;
case 'queue':
queue.replace(response.data);
break;
}
}
status.bind('change', function(event, attr, how, newVal, oldVal) {
if (attr == 'song') {
queue.updatePlaying(oldVal, newVal);
}
});
},
});

View File

@ -4,5 +4,6 @@
//= require_tree ./models/ //= require_tree ./models/
//= require_tree ./controls/ //= require_tree ./controls/
//= require_tree ./constructs/
//= require ./router.js //= require ./router.js
//= require ./application.js //= require ./application.js

View File

@ -28,12 +28,18 @@ class MPDClient < Sinatra::Base
end end
def self.send_status def self.send_status
status = JSON MPDConnection.status response = JSON({ type: 'status', data: MPDConnection.status })
settings.connections.each { |out| out << "data: #{status}\n\n" } settings.connections.each { |out| out << "data: #{response}\n\n" }
end
def self.send_queue
response = JSON({ type: 'queue', data: Song.queue.map(&:to_h) })
settings.connections.each { |out| out << "data: #{response}\n\n" }
end end
MPDConnection.mpd.on(:song) { |song| send_status } MPDConnection.mpd.on(:song) { |song| send_status }
MPDConnection.mpd.on(:state) { |state| send_status } MPDConnection.mpd.on(:state) { |state| send_status }
MPDConnection.mpd.on(:playlist) { |playlist| send_queue }
namespace '/api' do namespace '/api' do