Yesod, WebSockets and Persistent

I am trying to implement a server for turn-based play in Haskell. My choice would be to use Yesod for administration and meta-information (for example, what games the user participates in, etc.).

I would like to use web sockets so that a little overhead is in the game.

Looking at the ws-chat example, I'm not sure how to access Handad Monad and Persistent with it.

It would be nice to have some accounting code for the connections wrapped around a "normal" handler, which itself updates the database and informs the appropriate users.

+7
source share
2 answers

This is how I think it should look.

{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell, OverloadedStrings, GADTs, FlexibleContexts #-} module Main where import Control.Monad.IO.Class (liftIO) import Data.String (fromString) import Database.Persist import Database.Persist.TH import Database.Persist.Sqlite import Network.Wai.Application.Static (staticApp, defaultWebAppSettings, defaultFileServerSettings) import Network.Wai.Handler.Warp (runSettings, defaultSettings, settingsIntercept, settingsPort) import Network.Wai.Handler.WebSockets (intercept) import qualified Network.WebSockets as WS share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistUpperCase| Person name String age Int deriving Show |] ws :: WS.Request -> WS.WebSockets WS.Hybi10 () ws r = do WS.acceptRequest r liftIO $ runSqlite ":memory:" $ do runMigration migrateAll michaelId <- insert $ Person "Michael" 26 michael <- get michaelId liftIO $ print michael main :: IO () main = runSettings defaultSettings { settingsPort = 9160 , settingsIntercept = intercept $ ws } $ staticApp (defaultFileServerSettings $ fromString ".") 
+7
source

If you want to run the Handler monad yourself, you can use runFakeHandler .

+4
source

All Articles