From 2474e53f920181d9f3c4c305bf2ab8237167b4b2 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 25 Jan 2024 11:31:52 +0100 Subject: [PATCH] Add unliftio example in withPingPong docs See #246 --- src/Network/WebSockets/Connection/PingPong.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Network/WebSockets/Connection/PingPong.hs b/src/Network/WebSockets/Connection/PingPong.hs index cf3ae96..06a3795 100644 --- a/src/Network/WebSockets/Connection/PingPong.hs +++ b/src/Network/WebSockets/Connection/PingPong.hs @@ -40,9 +40,21 @@ defaultPingPongOptions = PingPongOptions { pingAction = return () } --- | Run an application with ping-pong enabled. Raises PongTimeout if a pong is not received. --- +-- | Run an application with ping-pong enabled. Raises 'PongTimeout' if a pong +-- is not received. +-- -- Can used with Client and Server connections. +-- +-- The implementation uses multiple threads, so if you want to call this from a +-- Monad other than 'IO', we recommend using +-- [unliftio](https://hackage.haskell.org/package/unliftio), e.g. using a +-- wrapper like this: +-- +-- > withPingPongUnlifted +-- > :: MonadUnliftIO m +-- > => PingPongOptions -> Connection -> (Connection -> m ()) -> m () +-- > withPingPongUnlifted options connection app = withRunInIO $ \run -> +-- > withPingPong options connection (run . app) withPingPong :: PingPongOptions -> Connection -> (Connection -> IO ()) -> IO () withPingPong options connection app = void $ withAsync (app connection) $ \appAsync -> do