diff options
-rw-r--r-- | main.hs | 30 |
1 files changed, 13 insertions, 17 deletions
@@ -33,8 +33,12 @@ warp_port = 10080 main :: IO () main = withSocketsDo $ do - h <- connectTo server_hostname (PortNumber server_port) - talk h `finally` hClose h + h <- connectTo server_hostname (PortNumber server_port) + (`finally` hClose h) $ do + hSetNewlineMode h universalNewlineMode + hSetBuffering h LineBuffering + + handshake h >> race (fromServer h) (warp h) >>= print handshake :: Handle -> IO () handshake h = do @@ -42,21 +46,13 @@ handshake h = do hPutStrLn h ("USER " ++ nick ++ " * 0 :" ++ nick) hPutStrLn h ("JOIN " ++ chan) -talk :: Handle -> IO () -talk h = do - hSetNewlineMode h universalNewlineMode - hSetBuffering h LineBuffering - handshake h - _ <- race fromServer (warp h) - return () - where - fromServer = forever $ do - line <- hGetLine h - --case line of - if (isPrefixOf "PING" (pack line)) then - hPutStrLn h (unpack (replace "PING" "PONG" (pack line))) - else - print line +fromServer :: Handle -> IO () +fromServer h = forever $ do + line <- hGetLine h + if (isPrefixOf "PING" (pack line)) then + hPutStrLn h (unpack (replace "PING" "PONG" (pack line))) + else + print line warp :: Handle -> IO () warp h = |