@@ -2,43 +2,24 @@
22module Main where
33
44import Control.Monad
5−import Control.Monad.Trans
65import Control.Concurrent (forkIO, threadDelay)
76import Control.Concurrent.Chan
87import Network.Wai.Handler.Warp (run)
98import Network.Wai
109import Network.HTTP.Types (statusOK)
11−import qualified Data.Conduit as C
1210import Data.Time.Clock.POSIX
1311import Blaze.ByteString.Builder.Char.Utf8 (fromString)
14−import Blaze.ByteString.Builder (Builder)
15−
16−import EventStream
1712
13+import Network.Wai.EventSource
1814
1915app :: Chan ServerEvent -> Application
2016app chan req =
2117 case pathInfo req of
2218 [] -> return $ ResponseFile statusOK [("Content-Type", "text/html")] "static/index.html" Nothing
23− ["es"] -> do
24− chan' <- liftIO $ dupChan chan
25− return $ res chan'
19+ ["es"] -> eventSourceApp chan req
2620 _ -> error "unexpected pathInfo"
2721
2822
29−res :: Chan ServerEvent -> Response
30−res chan = ResponseSource statusOK [("Content-Type", "text/event-stream")] (resE chan)
31−
32−
33−resE :: Chan ServerEvent -> C.Source IO Builder
34−resE chan =
35− C.sourceIO ign (\_ -> ign) (\_ -> fmap (f . eventToBuilder) $ readChan chan)
36− where
37− ign = return ()
38− f Nothing = C.Closed
39− f (Just b) = C.Open b
40−
41−
4223source :: Chan ServerEvent -> IO ()
4324source chan = forever $ do
4425 threadDelay 1000000