@@ -7,7 +7,7 @@ module Network.Wai.Handler.WebSockets
7 , runWebSockets
7 , runWebSockets
10−import Control.Exception (finally)
10+import Control.Exception (bracket)
11import Data.ByteString (ByteString)
11import Data.ByteString (ByteString)
12import qualified Data.ByteString.Char8 as BC
12import qualified Data.ByteString.Char8 as BC
13import qualified Data.ByteString.Lazy as BL
13import qualified Data.ByteString.Lazy as BL
@@ -95,20 +95,20 @@ runWebSockets :: WS.ConnectionOptions
95 -> IO ByteString
95 -> IO ByteString
96 -> (ByteString -> IO ())
96 -> (ByteString -> IO ())
98−runWebSockets opts req app src sink = do
98+runWebSockets opts req app src sink = bracket mkStream WS.close (app . pc)
99− stream <- WS.makeStream
101+ WS.makeStream
101− bs <- src
103+ bs <- src
102− return $ if BC.null bs then Nothing else Just bs)
104+ return $ if BC.null bs then Nothing else Just bs)
103− (\mbBl -> case mbBl of
105+ (\mbBl -> case mbBl of
104− Nothing -> return ()
106+ Nothing -> return ()
105− Just bl -> mapM_ sink (BL.toChunks bl))
107+ Just bl -> mapM_ sink (BL.toChunks bl))
107− let pc = WS.PendingConnection
108− { WS.pendingOptions = opts
109− , WS.pendingRequest = req
110− , WS.pendingOnAccept = \_ -> return ()
111− , WS.pendingStream = stream
114− app pc `finally` WS.close stream
109+ pc stream = WS.PendingConnection
110+ { WS.pendingOptions = opts
111+ , WS.pendingRequest = req
112+ , WS.pendingOnAccept = \_ -> return ()
113+ , WS.pendingStream = stream