Skip to content

Commit

Permalink
Add newSecureClientConnection and friends (#46)
Browse files Browse the repository at this point in the history
* Add newSecureClientConnection and friends

Addresses the closed issue #22.

* Deduplicate

* Update source/library/Wuss.hs

Co-authored-by: Taylor Fausak <[email protected]>

* Update source/library/Wuss.hs

Co-authored-by: Taylor Fausak <[email protected]>

---------

Co-authored-by: Taylor Fausak <[email protected]>
  • Loading branch information
bens and tfausak authored Jul 31, 2024
1 parent 80aa57c commit a6d64bd
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions source/library/Wuss.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@
-- See <https://github.com/tfausak/wuss/issues/18#issuecomment-990921703 this comment> for an example.
module Wuss
( runSecureClient,
newSecureClientConnection,
runSecureClientWith,
newSecureClientConnectionWith,
Config (..),
defaultConfig,
runSecureClientWithConfig,
newSecureClientConnectionWithConfig,
)
where

Expand Down Expand Up @@ -90,6 +93,25 @@ runSecureClient host port path app = do
let options = WebSockets.defaultConnectionOptions
runSecureClientWith host port path options [] app

-- | Build a new `Connection` from the client's point of view.
--
-- /WARNING/: Be sure to run the returned `IO ()` action after you are done
-- using the `Connection` in order to properly close the communication channel.
-- `runSecureClient` handles this for you, prefer to use it when possible.
newSecureClientConnection ::
(MonadIO.MonadIO m) =>
(Catch.MonadMask m) =>
-- | Host
Socket.HostName ->
-- | PortNumber
Socket.PortNumber ->
-- | Path
String.String ->
m (WebSockets.Connection, IO.IO ())
newSecureClientConnection host port path = do
let options = WebSockets.defaultConnectionOptions
newSecureClientConnectionWith host port path options []

-- |
-- A secure replacement for 'Network.WebSockets.runClientWith'.
--
Expand Down Expand Up @@ -147,6 +169,29 @@ runSecureClientWith host port path options headers app = do
let config = defaultConfig
runSecureClientWithConfig host port path config options headers app

-- | Build a new `Connection` from the client's point of view.
--
-- /WARNING/: Be sure to run the returned `IO ()` action after you are done
-- using the `Connection` in order to properly close the communication channel.
-- `runSecureClientWith` handles this for you, prefer to use it when possible.
newSecureClientConnectionWith ::
(MonadIO.MonadIO m) =>
(Catch.MonadMask m) =>
-- | Host
Socket.HostName ->
-- | PortNumber
Socket.PortNumber ->
-- | Path
String.String ->
-- | Options
WebSockets.ConnectionOptions ->
-- | Headers
WebSockets.Headers ->
m (WebSockets.Connection, IO.IO ())
newSecureClientConnectionWith host port path options headers = do
let config = defaultConfig
newSecureClientConnectionWithConfig host port path config options headers

-- | Configures a secure WebSocket connection.
newtype Config = Config
{ -- | How to get bytes from the connection. Typically
Expand Down Expand Up @@ -179,17 +224,46 @@ runSecureClientWithConfig ::
-- | Application
WebSockets.ClientApp a ->
m a
runSecureClientWithConfig host port path config options headers app = do
context <- MonadIO.liftIO Connection.initConnectionContext
runSecureClientWithConfig host port path config options headers app =
Catch.bracket
(newSecureClientConnectionWithConfig host port path config options headers)
(\(_, close) -> MonadIO.liftIO close)
(\(conn, _) -> MonadIO.liftIO (app conn))

-- | Build a new `Connection` from the client's point of view.
--
-- /WARNING/: Be sure to run the returned `IO ()` action after you are done
-- using the `Connection` in order to properly close the communication channel.
-- `runSecureClientWithConfig` handles this for you, prefer to use it when
-- possible.
newSecureClientConnectionWithConfig ::
(MonadIO.MonadIO m) =>
(Catch.MonadMask m) =>
-- | Host
Socket.HostName ->
-- | PortNumber
Socket.PortNumber ->
-- | Path
String.String ->
-- | Config
Config ->
-- | Options
WebSockets.ConnectionOptions ->
-- | Headers
WebSockets.Headers ->
m (WebSockets.Connection, IO.IO ())
newSecureClientConnectionWithConfig host port path config options headers = do
context <- MonadIO.liftIO Connection.initConnectionContext
Catch.bracketOnError
(MonadIO.liftIO $ Connection.connectTo context (connectionParams host port))
(MonadIO.liftIO . Connection.connectionClose)
( \connection -> MonadIO.liftIO $ do
stream <-
Stream.makeStream
(reader config connection)
(writer connection)
WebSockets.runClientWithStream stream host path options headers app
conn <- WebSockets.newClientConnection stream host path options headers
Applicative.pure (conn, Connection.connectionClose connection)
)

connectionParams ::
Expand Down

0 comments on commit a6d64bd

Please sign in to comment.