on master help sign in
Merge pull request #79 from obsidiansystems/ob-run-application-ws-reqs

ob run: Handle application websocket requests

commit3441cd673625ae62d2b6208ba634548855d18586 authorLuigy Leon <luichi.19@gmail.com> dateThu 14 Jun 2018, 12:10 (8 years ago) parentca195859029889
2 files changed+10−1
lib/run/obelisk-run.cabal+1 lib/run/src/Obelisk/Run.hs+9−1
lib/run/obelisk-run.cabal+1
@@ -24,6 +24,7 @@ library
2424 , streaming-commons
2525 , text
2626 , wai
27+ , wai-websockets
2728 , warp
2829 , websockets
2930 exposed-modules:
lib/run/src/Obelisk/Run.hs+9−1
@@ -25,6 +25,7 @@ import Network.Wai (Application)
2525import qualified Network.Wai as W
2626import Network.Wai.Handler.Warp
2727import Network.Wai.Handler.Warp.Internal (settingsHost, settingsPort)
28+import Network.Wai.Handler.WebSockets (isWebSocketsReq)
2829import Network.WebSockets (ConnectionOptions)
2930import Network.WebSockets.Connection (defaultConnectionOptions)
3031import Obelisk.ExecutableConfig (get)
@@ -80,10 +81,17 @@ obeliskApp :: ConnectionOptions -> StaticWidget () () -> Widget () () -> Applica
8081obeliskApp opts h b backend = do
8182 html <- BSLC.fromStrict <$> indexHtml h
8283 let entryPoint = mainWidget' b >> syncPoint
83− jsaddleOr opts entryPoint $ \req sendResponse -> case (W.requestMethod req, W.pathInfo req) of
84+ jsaddle <- jsaddleOr opts entryPoint $ \req sendResponse -> case (W.requestMethod req, W.pathInfo req) of
8485 ("GET", []) -> sendResponse $ W.responseLBS H.status200 [("Content-Type", "text/html")] html
8586 ("GET", ["jsaddle.js"]) -> sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] $ jsaddleJs False
8687 _ -> backend req sendResponse
88+ -- Workaround jsaddleOr wanting to handle all websockets requests without
89+ -- having a chance for run to proxy non jsaddle websocket requests to the
90+ -- backend.
91+ return $ \req sendResponse -> do
92+ if isWebSocketsReq req && not (null $ W.pathInfo req)
93+ then backend req sendResponse
94+ else jsaddle req sendResponse
8795
8896indexHtml :: StaticWidget () () -> IO ByteString
8997indexHtml h = do