This is regarding an attempt to get a WebSocket's input and output hooked up to a Coroutine.
The following function takes a Connection
then sets it up to emit
a Coroutine value when a message is received.
module Main where
import Prelude
import Control.Coroutine (emit, Producer, Consumer, await)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Var (($=))
import Control.Monad.Reader.Trans (lift)
import Control.Monad.Rec.Class (forever)
import WebSocket (WEBSOCKET, Connection(..), newWebSocket, URL(..), runMessage, runMessageEvent)
wsProducer :: Connection → Producer String (Eff _) Unit
wsProducer (Connection s) = s.onmessage $= emit <<< runMessage <<< runMessageEvent
The Producer
and Consumer
will be hooked up in Main
(the WebSocket connection will also be made there), that hasn't been written yet though, since the function above won't even typecheck.
How can this be made to typecheck, please? The fact it won't typecheck may mean there is a fundamental misunderstanding in the code above, if so an explanation with a code sample of a working solution would be very helpful.
Related: this answer about Halogen and WebSockets contains very similar code.