Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EchoClient example #72

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/EchoClient.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Main (main) where

import Data.Function ((&))
import Data.Word (Word8)
import Network.Socket (PortNumber)
import Streamly.Data.Stream.Prelude (Stream)

import qualified Streamly.Console.Stdio as Stdio
import qualified Streamly.Data.Array as Array
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Stream.Prelude as Stream
import qualified Streamly.Internal.Network.Inet.TCP as TCP
import qualified Streamly.Unicode.Stream as Unicode


remoteAddr :: (Word8,Word8,Word8,Word8)
remoteAddr = (127, 0, 0, 1)

remotePort :: PortNumber
remotePort = 8091

echo :: Stream IO ()
echo =
Stream.unfold Stdio.reader () -- Stream IO Word8
& split (== 10) Array.write -- Stream IO (Array Word8)
& TCP.pipeChunks remoteAddr remotePort -- Stream IO (Array Word8)
& Stream.unfoldMany Array.reader -- Stream IO Word8
& Unicode.decodeLatin1 -- Stream IO Char
& Stream.mapM putChar -- Stream IO ()

where

split p f = Stream.foldMany (Fold.takeEndBy p f)

main :: IO ()
main = Stream.fold Fold.drain echo
8 changes: 8 additions & 0 deletions streamly-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ executable EchoServer
else
buildable: False

executable EchoClient
import: exe-options-threaded
main-is: EchoClient.hs
if !impl(ghcjs)
buildable: True
else
buildable: False

executable FileSender
import: exe-options-threaded
main-is: FileSender.hs
Expand Down
Loading