Haskell相对较新,无法理解为什么以下不起作用。
我正在使用Aeson解析websocket查询中的对象并尝试打印它。
所有打印在所讨论的一个之前工作,但这个打印永远不会打印。我还尝试使用fromMaybe
并提供默认值的变体,但这也无济于事。
Command只是一个记录对象,包含各种对象的几个(可能是Map)。所有这些都有FromJSON实例等。
通常只有一张地图存在,但我打算稍后进行一些累积,这可能意味着一次传递了几个对象。
data Command = Command { _articles :: Maybe ArticleMapMaybe
, _clients :: Maybe ClientMapMaybe
, _orders :: Maybe OrderMapMaybe } deriving (Show, Eq, Generic)
instance FromJSON Command
instance ToJSON Command
-- take a request from the queue and service it
wsApplication :: MVar ServerState
-> WS.ServerApp
wsApplication state pending = do
putStrLn "Processing request"
let path = WS.requestPath (WS.pendingRequest pending)
putStrLn $ "path: " ++ (BS.unpack $ BS.fromStrict path)
conn <- WS.acceptRequest pending
query <- WS.receiveData conn
putStrLn $ "query: " ++ (T.unpack query)
let command = (A.decode $ BS.pack . T.unpack $ query) :: Maybe Command
print command -- this doesn't do anything!
let client = (query, conn) :: SocketClient
modifyMVar_ state $ return . addClient client
serverState <- readMVar state
putStrLn $ "connected clients: " ++ (show . length $ serverState ^. connectedClients)
perform state client command
结果:
Processing request
path: /api
query: {"_clients":null,"_orders":null,"_articles":{"dsa141":{"_notes":"ewq","_articleName":"Stellaris: Apocalypse"}}}
Processing request
path: /api
如果我删除“打印命令”行,则还会打印以下行(“连接的客户端”)。这意味着在打印命令时功能会阻塞..?显然,解析的结果不依赖于任何MVar或任何类型(并且“连接的客户端”打印,意味着服务器状态MVar未被阻止...)。