From: Samir Benmendil Date: Mon, 18 Jan 2021 23:49:47 +0000 (+0000) Subject: awesome/mpc: use a keep alive timer instead of ping on error X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/a08bd5453e003956de907743257c9706b5bbfe97 awesome/mpc: use a keep alive timer instead of ping on error Basically just test if we are connected every second and reconnect if we are not. --- diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index bd64fa8..2ef26b5 100644 --- a/awesome/widgets/mpc.lua +++ b/awesome/widgets/mpc.lua @@ -32,7 +32,7 @@ function mpc.new(host, port, password, error_handler, ...) _try_reconnect = false, _idle_subsystems = { ... } }, { __index = mpc }) - self:_connect() + self:connect() return self end @@ -52,7 +52,7 @@ function mpc:_error(err) end end -function mpc:_connect() +function mpc:connect() if self._connected then return end -- Reset all of our state self._reply_handlers = {} @@ -197,7 +197,7 @@ function mpc:_send(command, callback) end function mpc:send(...) - self:_connect() + self:connect() if not self._connected then return end diff --git a/awesome/widgets/mpd_widget.lua b/awesome/widgets/mpd_widget.lua index 24e4cd2..2e4b215 100644 --- a/awesome/widgets/mpd_widget.lua +++ b/awesome/widgets/mpd_widget.lua @@ -137,9 +137,7 @@ end function widget:error_handler(err) self._textbox:set_text("Error: " .. tostring(err)) - timer.start_new(10, function() - self._connection:send("ping") - end) + self._status = {} end function widget:run() @@ -172,6 +170,13 @@ function widget:run() end ) + self._keep_alive_timer = timer { + timeout = 1 + , autostart = true + , callback = function() self._connection:connect() end + , call_now = true + } + self._timer = timer({ timeout = 1 }) self._timer:connect_signal("timeout", function() self:popup_update() end)