diff options
author | Amir Saeid <amir@glgdgt.com> | 2021-07-02 18:52:25 +0100 |
---|---|---|
committer | Amir Saeid <amir@glgdgt.com> | 2021-07-13 08:40:45 +0100 |
commit | 084c16f5b3c49aa6bd1bc85bd14857e13cb20cac (patch) | |
tree | d645004a601725e1d52c1b16ff53dc8e9656b41e /src/Xmobar/Plugins | |
parent | fe92b6e5912c58890a2c393df73b6ab97c8d7449 (diff) | |
download | xmobar-084c16f5b3c49aa6bd1bc85bd14857e13cb20cac.tar.gz xmobar-084c16f5b3c49aa6bd1bc85bd14857e13cb20cac.tar.bz2 |
Reconnect on ConnectionClosed exception
Diffstat (limited to 'src/Xmobar/Plugins')
-rw-r--r-- | src/Xmobar/Plugins/Kraken.hs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/Xmobar/Plugins/Kraken.hs b/src/Xmobar/Plugins/Kraken.hs index afe04db..4a15199 100644 --- a/src/Xmobar/Plugins/Kraken.hs +++ b/src/Xmobar/Plugins/Kraken.hs @@ -4,17 +4,18 @@ module Xmobar.Plugins.Kraken (Kraken(..)) where import Control.Concurrent -import Control.Monad (forever, mzero, void) +import Control.Exception (catch) +import Control.Monad (forever, mzero, void, when) import Data.Aeson import Data.Aeson.Types (Parser, typeMismatch) import Data.List (sort) import Data.Text (Text, pack, unpack) import GHC.Generics +import Network.WebSockets (ClientApp, ConnectionException(ConnectionClosed), receiveData, sendTextData) import System.IO (hPutStrLn, stderr) import Text.Read (readMaybe) -import Xmobar.Run.Exec(Exec(..)) -import Network.WebSockets (ClientApp, receiveData, sendTextData) import Wuss (runSecureClient) +import Xmobar.Run.Exec(Exec(..)) import qualified Data.HashMap.Lazy as HML (lookup) import qualified Data.Map as Map @@ -27,7 +28,7 @@ instance Exec Kraken where alias (Kraken _ a) = a start (Kraken ps _) cb = do mvar <- newEmptyMVar - forkIO $ runSecureClient "ws.kraken.com" 443 "/" $ wsClientApp ps mvar + forkIO $ reconnectOnConnectionClose $ wsClientApp ps mvar let loop mv p = do v <- takeMVar mv let g = Map.insert (unpack $ fst v) (snd v) p @@ -40,6 +41,10 @@ instance Exec Kraken where display :: Map.Map String Double -> String display m = unwords $ sort $ map (\x -> fst x ++ ": " ++ show (snd x)) $ Map.toList m + reconnectOnConnectionClose :: ClientApp () -> IO () + reconnectOnConnectionClose ws = runSecureClient "ws.kraken.com" 443 "/" ws + `catch` (\e -> when (e == ConnectionClosed) $ reconnectOnConnectionClose ws) + wsClientApp :: [String] -> MVar (Text, Double) -> ClientApp () wsClientApp ps mvar connection = do sendTextData connection (encode Subscribe { event = "subscribe", pair = map pack ps, subscription = Subscription { name = "ticker" }}) |