【发布时间】:2021-06-07 14:56:48
【问题描述】:
在documentation for Hedis中,给出了一个使用pubSub函数的例子:
pubSub :: PubSub -> (Message -> IO PubSub) -> Redis ()
pubSub (subscribe ["chat"]) $ \msg -> do
putStrLn $ "Message from " ++ show (msgChannel msg)
return $ unsubscribe ["chat"]
鉴于pubSub 返回一个Redis (),是否有可能在代码的更下方,从回调外部重用这个msg 消息?
我从在 ScottyM monad 中运行的 Scotty 端点调用 pubSub,并且应该返回(长话短说)json msg
myEndpoint :: ScottyM ()
myEndpoint =
post "/hello/world" $ do
data :: MyData <- jsonData
runRedis redisConn $ do
pubSub (subscribe ["channel"]) $ \msg -> do
doSomethingWith msg
return $ unsubscribe ["channel"]
-- how is it possible to retrieve `msg` from here?
json $ somethingBuiltFromMsg
或者,有没有办法在回调中使用 Scotty 的 json?到目前为止,我无法做到这一点。
【问题讨论】:
-
是否应该将带有
json的行进一步向右缩进,使其位于 post 端点的 do 块中?
标签: haskell redis publish-subscribe scotty