Skip to content

Instantly share code, notes, and snippets.

@biilmann
Created August 26, 2011 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biilmann/1174102 to your computer and use it in GitHub Desktop.
Save biilmann/1174102 to your computer and use it in GitHub Desktop.
{-# 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