Created
August 26, 2011 18:41
-
-
Save biilmann/1174102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Applicative | |
import Control.Monad.Trans | |
import Control.Concurrent | |
import Snap.Types | |
import Snap.Util.FileServe | |
import Snap.Http.Server | |
import Snap.Iteratee | |
import qualified Data.ByteString as B | |
import Blaze.ByteString.Builder | |
main :: IO () | |
main = quickHttpServe $ | |
ifTop (serveFile "static/index.html") <|> | |
dir "static" (serveDirectory "static") <|> | |
route [ ("eventsource", eventHandler) ] | |
enum (Continue k) = do | |
liftIO $ putStrLn "Type a message" | |
x <- liftIO B.getLine | |
if x == "q" | |
then continue k | |
else k (Chunks [x]) >>== enum | |
enum step = returnI step | |
bsToBuilderEnum = mapEnum toByteString fromByteString | |
eventHandler :: Snap () | |
eventHandler = do | |
modifyResponse $ setContentType "text/event-stream" . | |
setResponseCode 200 . | |
setResponseBody (bsToBuilderEnum enum) | |
r <- getResponse | |
finishWith r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment