No title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
parseAuctionAction :: Text -> Maybe AuctionAction
parseAuctionAction jsonTxt = decode $ C.pack $ T.unpack jsonTxt

encodeAuctionAction :: AuctionAction -> Text
encodeAuctionAction a = X.toStrict $ D.decodeUtf8 $ encode a

broadcastAuctionAction ::
     Maybe AuctionAction -> MVar ServerState -> Maybe (IO ())
broadcastAuctionAction Nothing _ = Nothing
broadcastAuctionAction (Just auctionAction) state =
  Just $ readMVar state >>= broadcast (encodeAuctionAction auctionAction)

talk :: WS.Connection -> MVar ServerState -> Client -> IO ()
talk conn state (Client (name, _)) =
  forever $ do
    msg <- WS.receiveData conn
    serverState <- readMVar state
    readMVar state >>= broadcast msg
  where
    parseAuctionAction :: Text -> Maybe AuctionAction
    parsedAuctionAction = parseAuctionAction msg