@@ -1,7 +1,5 @@
11{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE ScopedTypeVariables #-}
4−{-# LANGUAGE RecursiveDo #-}
53-----------------------------------------------------------------------------
64--
75-- Module : Language.Javascript.JSaddle.WebSockets
@@ -19,98 +17,66 @@ module Language.Javascript.JSaddle.WebSockets (
1917 jsaddleOr
2018 , jsaddleApp
2119 , jsaddleWithAppOr
22− , jsaddleAppWithJs
23− , jsaddleAppWithJsOr
2420 , jsaddleAppPartial
25− , jsaddleJs
26− , jsaddleJs'
27− , debug
28− , debugOr
29− , debugWrapper
3021) where
3122
32−import Control.Monad (when, void, forever)
33−import Control.Concurrent (killThread, forkIO, threadDelay)
34−import Control.Exception (handle, AsyncException, throwIO, fromException, finally)
23+import Control.Monad (forever)
24+import Control.Concurrent (forkIO, threadDelay)
25+import Control.Exception (handle, AsyncException, throwIO, fromException)
3526
3627import Data.Monoid ((<>))
3728import Data.Aeson (encode, decode)
3829
3930import Network.Wai
40− (Middleware, lazyRequestBody, Application, Request, Response,
41− ResponseReceived)
31+ (requestBody, Application, Request, Response, ResponseReceived)
4232import Network.WebSockets
43− (defaultConnectionOptions, ConnectionOptions(..), sendTextData,
44− receiveDataMessage, acceptRequest, ServerApp, sendPing)
33+ (ConnectionOptions(..), sendTextData, receiveDataMessage,
34+ acceptRequest, ServerApp, sendPing)
35+import qualified Network.WebSockets as WS (DataMessage(..))
4536import Network.Wai.Handler.WebSockets (websocketsOr)
46−import Network.HTTP.Types (Status(..))
4737
48−import Language.Javascript.JSaddle.Types (JSM(..), JSContextRef(..))
38+import Language.Javascript.JSaddle.Types (JSM(..))
4939import qualified Network.Wai as W
50− (responseLBS, requestMethod, pathInfo, modifyResponse, responseStatus)
40+ (responseLBS, requestMethod, pathInfo)
5141import qualified Data.Text as T (pack)
5242import qualified Network.HTTP.Types as H
5343 (status403, status200)
54−import Language.Javascript.JSaddle.Run (syncPoint, runJavaScript)
44+import Language.Javascript.JSaddle.Run (runJavaScript)
5545import Language.Javascript.JSaddle.Run.Files (indexHtml, runBatch, ghcjsHelpers, initState)
56−import Language.Javascript.JSaddle.Debug
57− (removeContext, addContext)
5846import Data.Maybe (fromMaybe)
5947import qualified Data.Map as M (empty, insert, lookup)
60−import Data.IORef
61− (readIORef, newIORef, atomicModifyIORef')
62−import Data.ByteString.Lazy (ByteString)
63−import qualified Data.ByteString.Lazy as LBS
64− (toStrict, stripPrefix)
65−import Control.Concurrent.MVar
66− (tryTakeMVar, MVar, tryPutMVar, modifyMVar_, putMVar, takeMVar,
67− readMVar, newMVar, newEmptyMVar, modifyMVar)
68−import Network.Wai.Handler.Warp
69− (defaultSettings, setTimeout, setPort, runSettings)
70−import Foreign.Store (readStore, lookupStore, writeStore, Store(..))
71−import Language.Javascript.JSaddle (askJSM)
72−import Control.Monad.IO.Class (MonadIO(..))
73−
74−import Language.Javascript.JSaddle.WebSockets.Compat (getTextMessageByteString)
75−import qualified Data.Text.Encoding as T (decodeUtf8)
48+import Data.IORef (readIORef, newIORef, atomicModifyIORef')
49+import Data.ByteString.Lazy (fromStrict, ByteString)
50+import Data.Text (Text)
51+import Data.UUID.Types (toText)
52+import Data.UUID.V4 (nextRandom)
7653
7754jsaddleOr :: ConnectionOptions -> JSM () -> Application -> IO Application
7855jsaddleOr opts entryPoint otherApp = do
7956 syncHandlers <- newIORef M.empty
80− asyncHandlers <- newIORef M.empty
8157 let wsApp :: ServerApp
8258 wsApp pending_conn = do
8359 conn <- acceptRequest pending_conn
84− initMsg <- receiveDataMessage conn
85− case getTextMessageByteString initMsg of
86− Just "" -> do
87− rec (processResult, processSyncResult, start) <- runJavaScript (sendTextData conn . encode) $ do
88− syncKey <- T.pack . show . contextId <$> askJSM
89− liftIO $ atomicModifyIORef' syncHandlers (\m -> (M.insert syncKey processSyncResult m, ()))
90− liftIO $ atomicModifyIORef' asyncHandlers (\m -> (M.insert syncKey processResult m, ()))
91− liftIO $ sendTextData conn (encode syncKey)
92− entryPoint
93− start
94− waitTillClosed conn
95− Just syncKey ->
96− M.lookup (T.decodeUtf8 $ LBS.toStrict syncKey) <$> readIORef syncHandlers >>= \case
97− Nothing -> error "jsaddle missing sync message handler"
98− Just processResult ->
99− forever $
100− receiveDataMessage conn >>= \msg -> case getTextMessageByteString msg of
101− Just t ->
102− case decode t of
103− Nothing -> error $ "jsaddle Results decode failed : " <> show t
104− Just r -> processResult r
105− _ -> error "jsaddle WebSocket unexpected binary data"
106− _ -> error "jsaddle WebSocket unexpected binary data"
60+ (processResult, processSyncResult, start) <- runJavaScript (sendTextData conn . encode) entryPoint
61+ _ <- forkIO . forever $
62+ receiveDataMessage conn >>= \case
63+ (WS.Text t) ->
64+ case decode t of
65+ Nothing -> error $ "jsaddle Results decode failed : " <> show t
66+ Just r -> processResult r
67+ _ -> error "jsaddle WebSocket unexpected binary data"
68+ syncKey <- toText <$> nextRandom
69+ atomicModifyIORef' syncHandlers (\m -> (M.insert syncKey processSyncResult m, ()))
70+ sendTextData conn (encode syncKey)
71+ start
72+ waitTillClosed conn
10773
10874 -- Based on Network.WebSocket.forkPingThread
10975 waitTillClosed conn = ignore `handle` go 1
11076 where
11177 go :: Int -> IO ()
11278 go i = do
113− threadDelay (10 * 1000 * 1000)
79+ threadDelay (1 * 1000 * 1000)
11480 sendPing conn (T.pack $ show i)
11581 go (i + 1)
11682
@@ -121,8 +87,8 @@ jsaddleOr opts entryPoint otherApp = do
12187 syncHandler :: Application
12288 syncHandler req sendResponse = case (W.requestMethod req, W.pathInfo req) of
12389 ("POST", ["sync", syncKey]) -> do
124− body <- lazyRequestBody req
125− case decode body of
90+ body <- requestBody req
91+ case decode $ fromStrict body of
12692 Nothing -> error $ "jsaddle sync message decode failed : " <> show body
12793 Just result ->
12894 M.lookup syncKey <$> readIORef syncHandlers >>= \case
@@ -130,27 +96,15 @@ jsaddleOr opts entryPoint otherApp = do
13096 Just handler -> do
13197 next <- encode <$> handler result
13298 sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/json")] next
133− (method, _) -> (catch404 otherApp) req sendResponse
134− where catch404 = W.modifyResponse $ \resp ->
135− case (method, W.responseStatus resp) of
136− ("GET", Status 404 _) -> indexResponse
137− _ -> resp
99+ _ -> otherApp req sendResponse
138100 return $ websocketsOr opts wsApp syncHandler
139101
140102
141103jsaddleApp :: Application
142−jsaddleApp = jsaddleAppWithJs $ jsaddleJs False
143−
144−jsaddleAppWithJs :: ByteString -> Application
145−jsaddleAppWithJs js req sendResponse =
146− jsaddleAppWithJsOr js
147− (\_ _ -> sendResponse $ W.responseLBS H.status403 [("Content-Type", "text/plain")] "Forbidden")
148− req sendResponse
149−
150−jsaddleAppWithJsOr :: ByteString -> Application -> Application
151−jsaddleAppWithJsOr js otherApp req sendResponse =
152− fromMaybe (otherApp req sendResponse)
153− (jsaddleAppPartialWithJs js req sendResponse)
104+jsaddleApp req sendResponse =
105+ fromMaybe
106+ (sendResponse $ W.responseLBS H.status403 [("Content-Type", "text/plain")] "Forbidden")
107+ (jsaddleAppPartial req sendResponse)
154108
155109jsaddleWithAppOr :: ConnectionOptions -> JSM () -> Application -> IO Application
156110jsaddleWithAppOr opts entryPoint otherApp = jsaddleOr opts entryPoint $ \req sendResponse ->
@@ -158,82 +112,46 @@ jsaddleWithAppOr opts entryPoint otherApp = jsaddleOr opts entryPoint $ \req sen
158112 (jsaddleAppPartial req sendResponse))
159113
160114jsaddleAppPartial :: Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
161−jsaddleAppPartial = jsaddleAppPartialWithJs $ jsaddleJs False
162−
163−indexResponse :: Response
164−indexResponse = W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
165−
166−jsaddleAppPartialWithJs :: ByteString -> Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
167−jsaddleAppPartialWithJs js req sendResponse = case (W.requestMethod req, W.pathInfo req) of
168− ("GET", []) -> Just $ sendResponse indexResponse
169− ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] js
115+jsaddleAppPartial req sendResponse = case (W.requestMethod req, W.pathInfo req) of
116+ ("GET", []) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
117+ ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] jsaddleJs
170118 _ -> Nothing
171119
172−jsaddleJs :: Bool -> ByteString
173−jsaddleJs = jsaddleJs' Nothing
174−
175120-- Use this to generate this string for embedding
176121-- sed -e 's|\\|\\\\|g' -e 's|^| \\|' -e 's|$|\\n\\|' -e 's|"|\\"|g' data/jsaddle.js | pbcopy
177−jsaddleJs' :: Maybe ByteString -> Bool -> ByteString
178−jsaddleJs' jsaddleUri refreshOnLoad = "\
179− \if(typeof global !== \"undefined\" && typeof require === \"function\") {\n\
122+jsaddleJs :: ByteString
123+jsaddleJs = "\
124+ \if(typeof global !== \"undefined\") {\n\
180125 \ global.window = global;\n\
181126 \ global.WebSocket = require('ws');\n\
182127 \}\n\
183128 \\n\
184129 \var connect = function() {\n\
185− \ var wsaddress = "
186− <> maybe "window.location.protocol.replace('http', 'ws')+\"//\"+window.location.hostname+(window.location.port?(\":\"+window.location.port):\"\")"
187− (\ s -> "\"ws" <> s <> "\"")
188− (jsaddleUri >>= LBS.stripPrefix "http")
189− <> ";\n\
130+ \ var wsaddress = window.location.protocol.replace('http', 'ws')+\"//\"+window.location.hostname+(window.location.port?(\":\"+window.location.port):\"\");\n\
190131 \\n\
191− \ var ws0 = new WebSocket(wsaddress);\n\
132+ \ var ws = new WebSocket(wsaddress);\n\
192133 \ var syncKey = \"\";\n\
193134 \\n\
194− \ ws0.onopen = function(e) {\n\
195− \ ws0.send(\"\");\n\
196− \ var initialResults = [];\n\
197− \ var ws = {send: function(m) {initialResults.push(m);}};\n\
198− \ " <> initState <> "\n\
135+ \ ws.onopen = function(e) {\n\
136+ \ " <> initState <> "\
199137 \\n\
200− \ ws0.onmessage = function(e) {\n\
138+ \ ws.onmessage = function(e) {\n\
201139 \ var batch = JSON.parse(e.data);\n\
202− \ if(inCallback > 0) {\n\
203− \ asyncBatch = batch;\n\
204− \ return;\n\
205− \ }\n\
206140 \ if(typeof batch === \"string\") {\n\
207141 \ syncKey = batch;\n\
208− \ var ws1 = new WebSocket(wsaddress);\n\
209− \ ws1.onopen = function(e) {\n\
210− \ ws1.send(syncKey);\n\
211− \ initialResults.forEach(function(m){ ws1.send(m); });\n\
212− \ initialResults = null;\n\
213− \ ws = ws1;\n\
214− \ }\n" <>
215− (if refreshOnLoad
216− then " var xhr = new XMLHttpRequest();\n\
217− \ xhr.open('POST', '/reload/'+syncKey, true);\n\
218− \ xhr.onreadystatechange = function() {\n\
219− \ if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200)\n\
220− \ setTimeout(function(){window.location.reload();}, 100);\n\
221− \ };\n\
222− \ xhr.send();\n"
223− else "") <>
224− " return;\n\
142+ \ return;\n\
225143 \ }\n\
226144 \\n\
227145 \ " <> runBatch (\a -> "ws.send(JSON.stringify(" <> a <> "));")
228146 (Just (\a -> "(function(){\n\
229147 \ var xhr = new XMLHttpRequest();\n\
230− \ xhr.open('POST', '" <> fromMaybe "" jsaddleUri <> "/sync/'+syncKey, false);\n\
148+ \ xhr.open('POST', '/sync/'+syncKey, false);\n\
231149 \ xhr.setRequestHeader(\"Content-type\", \"application/json\");\n\
232150 \ xhr.send(JSON.stringify(" <> a <> "));\n\
233151 \ return JSON.parse(xhr.response);})()")) <> "\
234152 \ };\n\
235153 \ };\n\
236− \ ws0.onerror = function() {\n\
154+ \ ws.onerror = function() {\n\
237155 \ setTimeout(connect, 1000);\n\
238156 \ };\n\
239157 \}\n\
@@ -242,78 +160,3 @@ jsaddleJs' jsaddleUri refreshOnLoad = "\
242160 \connect();\n\
243161 \"
244162
245−-- | Start or restart the server.
246−-- To run this as part of every :reload use
247−-- > :def! reload (const $ return "::reload\nLanguage.Javascript.JSaddle.Warp.debug 3708 SomeMainModule.someMainFunction")
248−debug :: Int -> JSM () -> IO ()
249−debug port f = do
250− debugWrapper $ \withRefresh registerContext ->
251− runSettings (setPort port (setTimeout 3600 defaultSettings)) =<<
252− jsaddleOr defaultConnectionOptions (registerContext >> f >> syncPoint) (withRefresh $ jsaddleAppWithJs $ jsaddleJs True)
253− putStrLn $ "<a href=\"http://localhost:" <> show port <> "\">run</a>"
254−
255−debugOr :: Int -> JSM () -> Application -> IO ()
256−debugOr port f b = do
257− debugWrapper $ \withRefresh registerContext ->
258− runSettings (setPort port (setTimeout 3600 defaultSettings)) =<<
259− jsaddleOr defaultConnectionOptions (registerContext >> f >> syncPoint) (withRefresh $ jsaddleAppWithJsOr (jsaddleJs True) b)
260− putStrLn $ "<a href=\"http://localhost:" <> show port <> "\">run</a>"
261−
262−refreshMiddleware :: ((Response -> IO ResponseReceived) -> IO ResponseReceived) -> Middleware
263−refreshMiddleware refresh otherApp req sendResponse = case (W.requestMethod req, W.pathInfo req) of
264− ("POST", ["reload", _syncKey]) -> refresh sendResponse
265− _ -> otherApp req sendResponse
266−
267−debugWrapper :: (Middleware -> JSM () -> IO ()) -> IO ()
268−debugWrapper run = do
269− reloadMVar <- newEmptyMVar
270− reloadDoneMVars <- newMVar []
271− contexts <- newMVar []
272− let refresh sendResponse = do
273− reloadDone <- newEmptyMVar
274− modifyMVar_ reloadDoneMVars (return . (reloadDone:))
275− readMVar reloadMVar
276− r <- sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/json")] ("reload" :: ByteString)
277− putMVar reloadDone ()
278− return r
279− start :: Int -> IO (IO Int)
280− start expectedConnections = do
281− serverDone <- newEmptyMVar
282− ready <- newEmptyMVar
283− let registerContext :: JSM ()
284− registerContext = do
285− uuid <- contextId <$> askJSM
286− browsersConnected <- liftIO $ modifyMVar contexts (\ctxs -> return (uuid:ctxs, length ctxs + 1))
287− addContext
288− when (browsersConnected == expectedConnections) . void . liftIO $ tryPutMVar ready ()
289− thread <- forkIO $
290− finally (run (refreshMiddleware refresh) registerContext)
291− (putMVar serverDone ())
292− _ <- forkIO $ threadDelay 10000000 >> void (tryPutMVar ready ())
293− when (expectedConnections /= 0) $ takeMVar ready
294− return $ do
295− putMVar reloadMVar ()
296− ctxs <- takeMVar contexts
297− mapM_ removeContext ctxs
298− takeMVar reloadDoneMVars >>= mapM_ takeMVar
299− tryTakeMVar serverDone >>= \case
300− Nothing -> do
301− killThread thread
302− takeMVar serverDone
303− Just _ -> return ()
304− return $ length ctxs
305− restarter :: MVar (Int -> IO (IO Int)) -> IO Int -> IO ()
306− restarter mvar stop = do
307− start' <- takeMVar mvar
308− n <- stop
309− start' n >>= restarter mvar
310− lookupStore storeId >>= \case
311− Nothing -> do
312− restartMVar <- newMVar start
313− void . forkIO $ restarter restartMVar (return 0)
314− void $ writeStore (Store storeId) restartMVar
315− Just shutdownStore -> do
316− restartMVar :: MVar (Int -> IO (IO Int)) <- readStore shutdownStore
317− void $ tryTakeMVar restartMVar
318− putMVar restartMVar start
319− where storeId = 354