Automatic reconnect after the server dropped the connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index e6a1095..83204ff 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -158,12 +158,18 @@ class MPDClient
end
def connect(host = 'localhost', port = 6600)
- log.info("MPD connect #{host}, #{port}") if log
- if host.start_with?('/')
- @socket = UNIXSocket.new(host)
+ @host = host
+ @port = port
+ reconnect
+ end
+
+ def reconnect
+ log.info("MPD (re)connect #{host}, #{port}") if log
+ if @host.start_with?('/')
+ @socket = UNIXSocket.new(@host)
hello
else
- @socket = TCPSocket.new(host, port)
+ @socket = TCPSocket.new(@host, @port)
hello
end
end
@@ -213,7 +219,12 @@ class MPDClient
end
def write_line(line)
- @socket.puts line
+ begin
+ @socket.puts line
+ rescue Errno::EPIPE
+ reconnect
+ @socket.puts line
+ end
@socket.flush
end