/examples/Chat.hs

http://github.com/DanBurton/netspec · Haskell · 26 lines · 18 code · 3 blank · 5 comment · 6 complexity · f3c580c3559d25e990a2aed2c09d0e5d MD5 · raw file

  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Network.NetSpec
  3. import Network.NetSpec.Text
  4. -- Notice the restrictions NetSpec places on how you can communicate.
  5. -- Working within NetSpec isn't well-suited for all situations;
  6. -- it is specialized for situations where there is
  7. -- a deterministic sequence of communication.
  8. main :: IO ()
  9. main = runSpec ServerSpec {
  10. _ports = [PortNumber 5001, PortNumber 5002]
  11. , _begin = \hs -> do
  12. hs ! "Welcome to the one-for-one chat program."
  13. hs ! "Send a message in order to receive one."
  14. hs ! "Say \"bye\" to stop."
  15. , _loop = \[a, b] () -> do
  16. lineA <- receive a
  17. lineB <- receive b
  18. a ! lineB
  19. b ! lineA
  20. if lineA == "bye\r" || lineB == "bye\r"
  21. then stop_ else continue_
  22. , _end = \hs () -> hs ! "That's all folks."
  23. }