socketson-0.1.0.0: doc/sample/client/Main.hs
{-# LANGUAGE OverloadedStrings #-}
module Main
( main
) where
--------------------------------------------------------------------------------
import Control.Concurrent (forkIO)
import Control.Monad (forever, unless)
import Network.Socket (withSocketsDo)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.ByteString as B
import qualified Network.WebSockets as WS
import System.Environment (getArgs)
--------------------------------------------------------------------------------
app :: WS.ClientApp ()
app conn = do
putStrLn "Connected!"
-- Fork a thread that writes WS data to stdout
_ <- forkIO $ forever $ do
msg <- WS.receiveData conn :: IO B.ByteString
print msg
-- Read from stdin and write to WS
let loop = do
line <- T.getLine
unless (T.null line) $ WS.sendTextData conn line >> loop
loop
WS.sendClose conn ("Bye!" :: T.Text)
--------------------------------------------------------------------------------
main :: IO ()
main = do as <- getArgs
case as of
[port] -> withSocketsDo $ WS.runClient "localhost" (read port) "/open?id=sample-client" app
_ -> putStrLn "usage: socketson-sample-client <port>"