-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Haskell: Hello World ZeroMQ client and server
- Loading branch information
1 parent
bd2cf69
commit ad62589
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
content/docs/examples/haskell/zeromq4-haskell/hello_world_client.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
name: hello_world_server | ||
language: python | ||
library: pyzmq | ||
--- | ||
|
||
```haskell | ||
-- This lets us conviniently use efficient ByteString to send and recieve messages | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
{- | ||
Hello World client in Haskell | ||
Connects REQ socket to tcp://localhost:5555 | ||
Sends "Hello" to server, expects "World" back | ||
-} | ||
|
||
import System.ZMQ4.Monadic (runZMQ, socket, connect, send, receive, Socket, Req(..)) | ||
import Control.Monad (forM_) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Data.ByteString.Char8 (pack, unpack) | ||
|
||
main :: IO () | ||
main = runZMQ $ do | ||
-- Socket to talk to server | ||
reqSocket <- socket Req | ||
connect reqSocket "tcp://localhost:5555" | ||
|
||
-- Do 10 requests, waiting each time for a response | ||
forM_ [1..10] $ \i -> do | ||
liftIO $ putStrLn ("Sending request " ++ show i ++ "...") | ||
send reqSocket [] (pack "Hello") | ||
|
||
-- Get the reply | ||
message <- receive reqSocket | ||
liftIO $ putStrLn ("Received reply " ++ show i ++ " [" ++ unpack message ++ "]") | ||
``` |
37 changes: 37 additions & 0 deletions
37
content/docs/examples/haskell/zeromq4-haskell/hello_world_server.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
name: hello_world_server | ||
language: python | ||
library: pyzmq | ||
--- | ||
|
||
```haskell | ||
-- This lets us conviniently use efficient ByteString to send and recieve messages | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
{- | ||
Hello World server in Haskell | ||
Binds REP socket to tcp://*:5555 | ||
Expects "Hello" from client, replies with "World" | ||
-} | ||
|
||
import System.ZMQ4.Monadic (runZMQ, socket, bind, send, receive, Socket, Rep(..)) | ||
import Control.Monad (forever) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Control.Concurrent (threadDelay) | ||
import Data.ByteString.Char8 (pack, unpack) | ||
|
||
main :: IO () | ||
main = runZMQ $ do | ||
repSocket <- socket Rep | ||
bind repSocket "tcp://*:5555" | ||
forever $ do | ||
-- Wait for next request from client | ||
message <- receive repSocket | ||
liftIO $ putStrLn ("Received request: " ++ unpack message) | ||
|
||
-- Do some 'work' (waiting for 1 second here) | ||
liftIO $ threadDelay (1 * 1000000) | ||
|
||
-- Send reply back to client | ||
send repSocket [] (pack "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
headless: true | ||
--- |