Created
July 23, 2011 03:31
-
-
Save sebnow/1100960 to your computer and use it in GitHub Desktop.
Haskell ZeroMQ - Hello World!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Monad (forever) | |
import Control.Concurrent (forkIO) | |
import qualified Data.ByteString.Char8 as B | |
import System.ZMQ | |
connect' :: String -> (Socket Req -> IO ()) -> Context -> IO () | |
connect' endpoint f ctx = withSocket ctx Req (\s -> connect s endpoint >> f s) | |
bind' :: String -> (Socket Rep -> IO ()) -> Context -> IO () | |
bind' endpoint f ctx = withSocket ctx Rep (\s -> bind s endpoint >> f s) | |
requester :: Socket a -> IO () | |
requester s = forever $ do | |
B.putStrLn $ B.concat [" Sending: ", request] | |
send s request [] | |
reply <- receive s [] | |
B.putStrLn $ B.concat ["Received: ", reply] | |
where request = "Hello world!" :: B.ByteString | |
replier :: Socket a -> IO () | |
replier s = forever $ do | |
msg <- receive s [] | |
B.putStrLn $ B.concat ["Received: ", msg] | |
B.putStrLn $ B.concat ["Replying: ", reply] | |
send s reply [] | |
where reply = "Hello!" :: B.ByteString | |
main = withContext 1 $ \ctx -> do | |
forkIO $ bind' "tcp://*:5555" replier ctx | |
connect' "tcp://localhost:5555" requester ctx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment