为什么我不能在`do`块中抽出时间?

cie*_*bor 0 io time haskell websocket

我有这个功能,负责网络聊天中的通信.一切正常没有t <- getClockTime(这里是代码):

talk :: WS.Protocol p => MVar State -> Client -> WS.WebSockets p ()
talk state client@(user, sink) = flip WS.catchWsError catchDisconnect $ 
  forever $ do
    t <- getClockTime
    msg <- WS.receiveData
    case () of
      () | T.unpack(msg) == "#list" -> liftIO $ readMVar state >>= listClients client
        | T.unpack(msg) == "#time" -> liftIO (sendTime client)
        | "#name" `T.isPrefixOf` msg -> liftIO (command msg client)
        | otherwise -> liftIO $ readMVar state >>= broadcast
          (user `mappend` msg)
  where
    catchDisconnect e = case fromException e of
        Just WS.ConnectionClosed -> liftIO $ modifyMVar_ state $ \s -> do
            let new_state = removeClient client s
            return new_state
        _ -> return ()
Run Code Online (Sandbox Code Playgroud)

随着t <- getClockTime我得到的错误:

Couldn't match type `WS.WebSockets p0' with `IO'
Expected type: IO Text
  Actual type: WS.WebSockets p0 Text
In a stmt of a 'do' block: msg <- WS.receiveData
In the second argument of `($)', namely
  `do { t <- getClockTime;
        msg <- WS.receiveData;
        case () of {
          ()
            | T.unpack (msg) == "#list"
            -> liftIO $ readMVar state >>= listClients client
            | T.unpack (msg) == "#time" -> liftIO (sendTime client)
            | "#name" `T.isPrefixOf` msg -> liftIO (command msg client)
            | otherwise
            -> liftIO $ readMVar state >>= broadcast (user `mappend` msg) } }'
In the second argument of `($)', namely
  `forever
   $ do { t <- getClockTime;
          msg <- WS.receiveData;
          case () of {
            ()
              | T.unpack (msg) == "#list"
              -> liftIO $ readMVar state >>= listClients client
              | T.unpack (msg) == "#time" -> liftIO (sendTime client)
              | "#name" `T.isPrefixOf` msg -> liftIO (command msg client)
              | otherwise
              -> liftIO $ readMVar state >>= broadcast (user `mappend` msg) } }'
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到这个时间并将其与msg连接起来?

sha*_*haf 11

因为你的monad不是IO.我不知道WS.WebSockets它是什么,但如果它是这个,它是一个实例MonadIO,所以你可以使用.我建议在这类代码中阅读更多关于monad变换器的内容,因为提升会有很多变化.liftIOgetClockTime