From 084c16f5b3c49aa6bd1bc85bd14857e13cb20cac Mon Sep 17 00:00:00 2001 From: Amir Saeid Date: Fri, 2 Jul 2021 18:52:25 +0100 Subject: Reconnect on ConnectionClosed exception --- src/Xmobar/Plugins/Kraken.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/Xmobar') 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" }}) -- cgit v1.2.3