Skip to content

Commit

Permalink
Merge pull request tfausak#24 from L0neGamer/monadclasses
Browse files Browse the repository at this point in the history
use MonadIO to generalise the library a bit.
  • Loading branch information
tfausak authored Jan 23, 2023
2 parents c293b1b + 25adaf2 commit 1f886b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
32 changes: 21 additions & 11 deletions source/library/Wuss.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ module Wuss
, runSecureClientWithConfig
) where

import Prelude (($), (.))

import qualified Control.Applicative as Applicative
import qualified Control.Exception as Exception
import qualified Control.Monad.IO.Class as MonadIO
import qualified Control.Monad.Catch as Catch
import qualified Data.Bool as Bool
import qualified Data.ByteString as StrictBytes
import qualified Data.ByteString.Lazy as LazyBytes
Expand All @@ -74,11 +78,13 @@ import qualified System.IO.Error as IO.Error
>>> runSecureClient "echo.websocket.org" 443 "/" app
-}
runSecureClient
:: Socket.HostName -- ^ Host
:: MonadIO.MonadIO m
=> Catch.MonadMask m
=> Socket.HostName -- ^ Host
-> Socket.PortNumber -- ^ Port
-> String.String -- ^ Path
-> WebSockets.ClientApp a -- ^ Application
-> IO.IO a
-> m a
runSecureClient host port path app = do
let options = WebSockets.defaultConnectionOptions
runSecureClientWith host port path options [] app
Expand Down Expand Up @@ -123,13 +129,15 @@ runSecureClient host port path app = do
> return ()
-}
runSecureClientWith
:: Socket.HostName -- ^ Host
:: MonadIO.MonadIO m
=> Catch.MonadMask m
=> Socket.HostName -- ^ Host
-> Socket.PortNumber -- ^ Port
-> String.String -- ^ Path
-> WebSockets.ConnectionOptions -- ^ Options
-> WebSockets.Headers -- ^ Headers
-> WebSockets.ClientApp a -- ^ Application
-> IO.IO a
-> m a
runSecureClientWith host port path options headers app = do
let config = defaultConfig
runSecureClientWithConfig host port path config options headers app
Expand All @@ -152,20 +160,22 @@ defaultConfig = do

-- | Runs a secure WebSockets client with the given 'Config'.
runSecureClientWithConfig
:: Socket.HostName -- ^ Host
:: MonadIO.MonadIO m
=> Catch.MonadMask m
=> Socket.HostName -- ^ Host
-> Socket.PortNumber -- ^ Port
-> String.String -- ^ Path
-> Config -- ^ Config
-> WebSockets.ConnectionOptions -- ^ Options
-> WebSockets.Headers -- ^ Headers
-> WebSockets.ClientApp a -- ^ Application
-> IO.IO a
-> m a
runSecureClientWithConfig host port path config options headers app = do
context <- Connection.initConnectionContext
Exception.bracket
(Connection.connectTo context (connectionParams host port))
Connection.connectionClose
(\connection -> do
context <- MonadIO.liftIO Connection.initConnectionContext
Catch.bracket
(MonadIO.liftIO $ Connection.connectTo context (connectionParams host port))
(MonadIO.liftIO . Connection.connectionClose)
(\connection -> MonadIO.liftIO $ do
stream <- Stream.makeStream
(reader config connection)
(writer connection)
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-17.0
resolver: lts-18.28
1 change: 1 addition & 0 deletions wuss.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ common library
, connection >= 0.3.1 && < 0.4
, network >= 3.1.1 && < 3.2
, websockets >= 0.12.7 && < 0.13
, exceptions >= 0.10 && < 0.11
default-language: Haskell2010
ghc-options:
-Weverything
Expand Down

0 comments on commit 1f886b7

Please sign in to comment.