haskell - Using a monad inside the IO monad -


is there opposite of liftio? i'm using websockets, , want able listen messages server in separate thread. here's i'm doing:

import network.websockets import qualified data.text t import control.monad.io.class import control.monad import control.concurrent import control.applicative  printmessages :: websockets hybi00 () printmessages = forever $     resp <- receivedatamessage      liftio $ print resp  run :: websockets hybi00 () run =     liftio . forkio $ printmessages     forever $       line <- liftio getline       sendtextdata . t.pack $ line  main = connect "0.0.0.0" 8080 "/" run 

so printmessages listens messages server , keeps printing them out. problem is, forkio expects function returns io (). there way me run printmessages in io monad?

if i'm understanding right, reason want receive messages in thread because main thread waiting user input send.

from @ the documentation, seems you'll have easier time if reverse roles of threads: receive in main thread, , send asynchronously other.

then can use getsink :: protocol p => websockets p (sink p) grab sink before forking, can use sendsink :: sink p -> message p -> io () lives in io, avoiding whole problem of mixing monads.

in other words, restructure code this:

sendmessages :: sink hybi00 -> io () sendmessages sink = forever $     line <- getline     let msg = textdata . t.pack $ line     sendsink sink msg  run :: websockets hybi00 () run =     sink <- getsink     liftio . forkio $ sendmessages sink     forever $       resp <- receivedatamessage        liftio $ print resp  main = connect "0.0.0.0" 8080 "/" run 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -