I want to finish the race_ function but I don't know how to do that.
This is the code I have right now:
mainLoop :: Handle -> String -> IO ()
mainLoop handle username = do
_ <- race_ fromServer toServer
putStrLn "Race is done"
return ()
where
fromServer = forever $ do
line <- hGetLine handle
putStrLn $ "Server: " ++ line
toServer = do
line <- getLine
case line of
":quit" -> do
--hPutStrLn handle "blabla"
return ()
_ -> do
hPutStrLn handle $ line
toServer
If I uncomment the line hPutStrLn handle "blabla" the user will see Race is done when he types :quit. Without that line the program just hang when the user types :quit.
I feels wrong to use hPutStrLn handle "blabla" just to finish the race_. I assume that there exists a better function to finish the race_ function. Any idea?
I find it strange that a simple return () doesn't work since both hPutStrLn and return returns an empty IO action.
Full context can be found here. The handle is created by handle <- connectTo "localhost" (PortNumber 4000). The server is created in server.hs. I'm running this code on Windows 7 in "Git bash" (msysgit) with runhaskell.
Used in context
import System.IO
import Control.Concurrent.Async
import Control.Monad
main = do
h <- openFile "./a" ReadWriteMode
mainLoop h "user"
hClose h
this works fine also without hPutStrLn - what did you do? (where ./a is a large file, like "dmesg > a")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With