您好,我需要编写函数,而不是通过 tcp 发送和接收消息,如果连接断开,则自动重新连接。消息取自 STM Channel
f ch a b =
h <- connectTo a b
forever $ do
c <- atomically $ readTChan ch
{- do smth with c -}
`catch` (const $ f ch a b)
我的问题是,如果连接断开,我会丢失从频道中读取的“c”。所以在catch
子句中我不想像 unGetTchan 这样的东西,但在这段代码中,'c' 没有被捕获。你能建议“haskellic”的方式来做这样的事情吗?
FUZxxl 消息后更新
在 FUZxxl 发表评论后,我将函数重写为下一个形式
fun ch a b = do
h <- connectTo a b
forever $ do
c <- atomically $ readTChan chan
do
{- do smth with c -}
`catch` (const $ do
atomically $ unGetTChan chan c
fun chan con
)
现在它对我有用。谢谢