首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PureScript卤素和websockets

PureScript卤素和websockets
EN

Stack Overflow用户
提问于 2016-07-14 09:21:08
回答 1查看 1.3K关注 0票数 8

我正在尝试将purescript-halogen与websockets结合使用,但经过多次尝试,我无法使它们一起工作。

我已经看到了this question on Thermite and websockets和菲尔关于Driver函数的答案。卤素也有一个Driver函数,但是我需要用Aff效应运行Driver函数,而purescript-websockets-simple使用Eff效应。

我不知道如何将websocket包的同步回调转换为运行在Aff monad中的异步代码。我需要使用AVar吗?我需要purescript-coroutines-aff吗?如果是的话,我该如何将这些部件连接在一起呢?

预先感谢任何正确方向的指点!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-14 12:29:24

在这种情况下,您确实希望使用purescript-aff-coroutines。这将为您提供一个协同线Producer,然后您可以连接到一个将消息推入驱动程序的Consumer

代码语言:javascript
复制
module Main where

import Prelude

import Control.Coroutine (Producer, Consumer, consumer, runProcess, ($$))
import Control.Coroutine.Aff (produce)
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.AVar (AVAR)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (EXCEPTION)
import Control.Monad.Eff.Var (($=))

import Data.Array as Array
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))

import Halogen as H
import Halogen.HTML.Indexed as HH
import Halogen.Util (runHalogenAff, awaitBody)

import WebSocket (WEBSOCKET, Connection(..), Message(..), URL(..), runMessageEvent, runMessage, newWebSocket)

----------------------------------------------------------------------------
-- Halogen component. This just displays a list of messages and has a query
-- to accept new messages.
----------------------------------------------------------------------------

type State = { messages :: Array String }

initialState :: State
initialState = { messages: [] }

data Query a = AddMessage String a

ui :: forall g. H.Component State Query g
ui = H.component { render, eval }
  where
  render :: State -> H.ComponentHTML Query
  render state =
    HH.ol_ $ map (\msg -> HH.li_ [ HH.text msg ]) state.messages

  eval :: Query ~> H.ComponentDSL State Query g
  eval (AddMessage msg next) = do
    H.modify \st -> { messages: st.messages `Array.snoc` msg }
    pure next

----------------------------------------------------------------------------
-- Websocket coroutine producer. This uses `purescript-aff-coroutines` to
-- create a producer of messages from a websocket.
----------------------------------------------------------------------------

wsProducer :: forall eff. Producer String (Aff (avar :: AVAR, err :: EXCEPTION, ws :: WEBSOCKET | eff)) Unit
wsProducer = produce \emit -> do
  Connection socket <- newWebSocket (URL "ws://echo.websocket.org") []

  -- This part is probably unnecessary in the real world, but it gives us 
  -- some messages to consume when using the echo service
  socket.onopen $= \event -> do
    socket.send (Message "hello")
    socket.send (Message "something")
    socket.send (Message "goodbye")

  socket.onmessage $= \event -> do
    emit $ Left $ runMessage (runMessageEvent event)

----------------------------------------------------------------------------
-- Coroutine consumer. This accepts a Halogen driver function and sends
-- `AddMessage` queries in when the coroutine consumes an input.
----------------------------------------------------------------------------

wsConsumer
  :: forall eff
   . (Query ~> Aff (H.HalogenEffects (ws :: WEBSOCKET | eff)))
  -> Consumer String (Aff (H.HalogenEffects (ws :: WEBSOCKET | eff))) Unit
wsConsumer driver = consumer \msg -> do
  driver $ H.action $ AddMessage msg
  pure Nothing

----------------------------------------------------------------------------
-- Normal Halogen-style `main`, the only addition is a use of `runProcess`
-- to connect the producer and consumer and start sending messages to the
-- Halogen component.
----------------------------------------------------------------------------

main :: forall eff. Eff (H.HalogenEffects (ws :: WEBSOCKET | eff)) Unit
main = runHalogenAff do
  body <- awaitBody
  driver <- H.runUI ui initialState body
  runProcess (wsProducer $$ wsConsumer driver)
  pure unit

这将为您提供一页几乎立即打印的页面:

  1. 你好
  2. 某物
  3. 再见

但它能做你想做的一切,诚实!如果你使用生产者的“真实”来源,你会得到更多的东西,你需要什么。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38370322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档