April 2012
Intermediate to advanced
294 pages
6h 27m
English
This example will tie together a few different ideas. We’ll start with a chat subsite, which allows us to embed a chat widget on any page. We’ll use the HTML5 event source API to handle sending events from the server to the client.
-- @Chat.hs{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes,TemplateHaskell, FlexibleInstances, MultiParamTypeClasses,FlexibleContexts#-}-- | Thismoduledefinesasubsitethatallowsyoutoinsertachatboxon-- any page of your site. It uses eventsource for sending the messages from-- the server to the browser.moduleChatwhereimportYesodimportControl.Concurrent.Chan(Chan,dupChan,writeChan)importData.Text(Text)importNetwork.Wai.EventSource(ServerEvent(..),eventSourceAppChan)importLanguage.Haskell.TH.Syntax(Type(VarT),Pred(ClassP),mkName)importBlaze.ByteString.Builder.Char.Utf8(fromText)importData.Monoid(mappend)-- | Our subsite foundation. We keep a channel of events that all connections-- will share.dataChat=Chat(ChanServerEvent)-- | We need to know how to check if a user is logged in and how to get-- his/her username (for printing messages).class(Yesodmaster,RenderMessagemasterFormMessage)=>YesodChatmasterwheregetUserName::GHandlersubmasterTextisLoggedIn::GHandlersubmasterBool-- Now we set up our subsite. The first argument is the subsite, very similar-- to how we've used mkYesod in the past. The second argument ...