我正在使用 Pipes-2.1.0 包和 zeromq3-haskell 包来构建一个小消息管道。除了我无法理解 Frames 的最终确定之外,一切似乎都进展顺利。
在接下来的框架中,我获得了两个资源;zeromq 上下文和 zeromq 套接字。然后我不断地等待消息(以 的形式ByteStrings
)在 zeromq 套接字上发布。
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}
module PipesZeroMQ where
import Control.Frame
import Control.IMonad.Do
import Control.IMonad.Trans
import qualified Control.Monad as M
import Data.ByteString (ByteString)
import Data.String
import Prelude hiding (Monad(..))
import qualified System.ZMQ3 as ZMQ
type Address = String
fromList :: (M.Monad m) => [b] -> Frame b m (M a) (M a) ()
fromList xs = mapMR_ yield xs
publisher :: Address -> Frame Void IO (M ByteString) C ()
publisher addr = do
c <- liftU $ ZMQ.init 1
s <-liftU $ ZMQ.socket c ZMQ.Pub
liftU $ ZMQ.bind s addr
liftU $ print "Socket open for business!!!"
foreverR $ do
bs <- await
finallyF (ZMQ.close s M.>> ZMQ.term c M.>> print "ZMQ socket closed") $ do
(liftU $ ZMQ.send s [] bs)
(liftU (print "Sending message"))
现在如果我试试这个:
λ> runFrame $ (publisher localAddress) <-< (fromList ["This", "that", "that"] >> close)
我明白了:
"Socket open for business"
"Sending message"
"ZMQ socket closed"
*** Exception: ZMQError { errno = 88, source = "send", message = "Socket operation on non-socket" }
publisher
收到后完成,但一个BytesString
。
为什么会这样?
我对在 Pipes-2.1.0 中使用 Frames 进行最终确定有什么误解?
如果我开始攻击外面的树,它还有机会吗?