Skip to content

Commit

Permalink
server: add handleException
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jan 9, 2024
1 parent 4f495aa commit 3bd7a69
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Network/WebSockets/Simple/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Network.WebSockets.Simple.Server
)
where

import Control.Exception (throwIO)
import Control.Exception.Safe (SomeException, handle, throwIO)
import Control.Monad (when)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (unpack)
Expand All @@ -22,7 +22,8 @@ import Network.WebSockets.Simple.Utils qualified as Utils
data Options a = Options
{ handlePendingConnection :: (ClientConnection a) => WS.PendingConnection -> IO (Maybe a),
pingPongOptions :: WS.PingPongOptions -> IO WS.PingPongOptions,
messageLimit :: Int
messageLimit :: Int,
handleException :: WS.PendingConnection -> SomeException -> IO ()
}

--
Expand All @@ -31,7 +32,8 @@ defaultOptions =
Options
{ handlePendingConnection = (fmap Just) . WS.acceptRequest,
pingPongOptions = return,
messageLimit = 10000
messageLimit = 10000,
handleException = \_ _ -> return ()
}

class ClientConnection a where
Expand All @@ -53,7 +55,7 @@ run uriBS options app receiveApp = do
WS.runServerWithOptions serverOptions (application pingpongOpts)
where
application :: PingPong.PingPongOptions -> WS.ServerApp
application pingpongOpts pendingConnection = do
application pingpongOpts pendingConnection = handle (handleException options pendingConnection) $ do
maybeClient <- handlePendingConnection options pendingConnection
for_ maybeClient $ \client -> PingPong.withPingPong pingpongOpts (getConnection client) $ \_ ->
Session.run (messageLimit options) (getConnection client) (app client) (receiveApp client)
Expand Down
2 changes: 1 addition & 1 deletion websockets-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ common common
wuss >= 2.0.1.7,
unliftio-core,
bytestring,
exceptions,
safe-exceptions,
stamina

default-extensions: OverloadedStrings
Expand Down

0 comments on commit 3bd7a69

Please sign in to comment.