websockets 0.5.2.0 → 0.5.2.1
raw patch · 3 files changed
+17/−7 lines, 3 files
Files
- src/Network/WebSockets/Monad.hs +1/−1
- src/Network/WebSockets/Util/PubSub.hs +15/−5
- websockets.cabal +1/−1
src/Network/WebSockets/Monad.hs view
@@ -205,7 +205,7 @@ -- connection is closed. sendSink :: Sink p -> Message p -> IO () sendSink sink msg = modifyMVar_ (unSink sink) $ \iter -> do- step <- E.runIteratee $ singleton msg $$ iter+ step <- E.runIteratee $ E.catchError (singleton msg $$ iter) throw return $ E.returnI step -- | In case the user of the library wants to do asynchronous sending to the
src/Network/WebSockets/Util/PubSub.hs view
@@ -10,7 +10,7 @@ -- -- * Push new updates from the server using the 'publish' call ---{-# LANGUAGE Rank2Types #-}+{-# LANGUAGE Rank2Types, ScopedTypeVariables #-} module Network.WebSockets.Util.PubSub ( PubSub , newPubSub@@ -19,9 +19,11 @@ ) where import Control.Applicative ((<$>))-import Control.Monad (forM_, forever)+import Control.Exception (IOException, handle)+import Control.Monad (foldM, forever) import Control.Monad.Trans (liftIO) import Data.IntMap (IntMap)+import Data.List (foldl') import qualified Control.Concurrent.MVar as MV import qualified Data.IntMap as IM@@ -52,9 +54,17 @@ -- | Broadcast a message to all connected clients publish :: PubSub p -> Message p -> IO ()-publish (PubSub mvar) msg = do- sinks <- pubSubSinks <$> MV.readMVar mvar - forM_ (IM.toList sinks) $ \(_, s) -> sendSink s msg+publish (PubSub mvar) msg = MV.modifyMVar_ mvar $ \pubSub -> do+ -- Take care to detect and remove broken clients+ broken <- foldM publish' [] (IM.toList $ pubSubSinks pubSub)+ return $ foldl' (\p b -> removeClient b p) pubSub broken+ where+ -- Publish the message to a single client, add it to the broken list if an+ -- IOException occurs+ publish' broken (i, s) =+ handle (\(_ :: IOException) -> return (i : broken)) $ do+ sendSink s msg+ return broken -- | Blocks forever subscribe :: Protocol p => PubSub p -> WebSockets p ()
websockets.cabal view
@@ -1,5 +1,5 @@ Name: websockets-Version: 0.5.2.0+Version: 0.5.2.1 Synopsis: A sensible and clean way to write WebSocket-capable servers in Haskell.