@@ -2,43 +2,24 @@
2module Main where
2module Main where
4import Control.Monad
4import Control.Monad
5−import Control.Monad.Trans
6import Control.Concurrent (forkIO, threadDelay)
5import Control.Concurrent (forkIO, threadDelay)
7import Control.Concurrent.Chan
6import Control.Concurrent.Chan
8import Network.Wai.Handler.Warp (run)
7import Network.Wai.Handler.Warp (run)
9import Network.Wai
8import Network.Wai
10import Network.HTTP.Types (statusOK)
9import Network.HTTP.Types (statusOK)
11−import qualified Data.Conduit as C
12import Data.Time.Clock.POSIX
10import Data.Time.Clock.POSIX
13import Blaze.ByteString.Builder.Char.Utf8 (fromString)
11import Blaze.ByteString.Builder.Char.Utf8 (fromString)
14−import Blaze.ByteString.Builder (Builder)
13+import Network.Wai.EventSource
19app :: Chan ServerEvent -> Application
15app :: Chan ServerEvent -> Application
20app chan req =
16app chan req =
21 case pathInfo req of
17 case pathInfo req of
22 [] -> return $ ResponseFile statusOK [("Content-Type", "text/html")] "static/index.html" Nothing
18 [] -> return $ ResponseFile statusOK [("Content-Type", "text/html")] "static/index.html" Nothing
23− ["es"] -> do
19+ ["es"] -> eventSourceApp chan req
24− chan' <- liftIO $ dupChan chan
26 _ -> error "unexpected pathInfo"
20 _ -> error "unexpected pathInfo"
29−res :: Chan ServerEvent -> Response
30−res chan = ResponseSource statusOK [("Content-Type", "text/event-stream")] (resE chan)
33−resE :: Chan ServerEvent -> C.Source IO Builder
35− C.sourceIO ign (\_ -> ign) (\_ -> fmap (f . eventToBuilder) $ readChan chan)
39− f (Just b) = C.Open b
42source :: Chan ServerEvent -> IO ()
23source :: Chan ServerEvent -> IO ()
43source chan = forever $ do
24source chan = forever $ do
44 threadDelay 1000000
25 threadDelay 1000000