0

根据这个主题:https ://stackoverflow.com/questions/14494922/examples-of-websocket-usage-in-haskell我有我的第一个问题。为什么 WebSockets 库的官方示例不能在我的机器上运行?

import Data.Char (isPunctuation, isSpace)
import Data.Monoid (mappend)
import Data.Text (Text)
import Control.Exception (fromException)
import Control.Monad (forM_, forever)
import Control.Concurrent (MVar, newMVar, modifyMVar_, readMVar)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Network.WebSockets

meow :: TextProtocol p => WebSockets p ()
meow = forever $ do
    msg <- receiveData
    sendTextData $ msg `T.append` ", meow."

main :: IO ()
main = runServer "0.0.0.0" 8000 meow

我得到:

ciembor@freedom ~> ghc hchat.hs; and ./hchat
[1 of 1] Compiling Main             ( hchat.hs, hchat.o )

hchat.hs:15:35:
    Couldn't match expected type `Text' with actual type `[Char]'
    In the second argument of `T.append', namely `", meow."'
    In the second argument of `($)', namely `msg `T.append` ", meow."'
    In a stmt of a 'do' block: sendTextData $ msg `T.append` ", meow."

hchat.hs:18:33:
    Couldn't match expected type `Request -> WebSockets p0 ()'
                with actual type `WebSockets p1 ()'
    In the third argument of `runServer', namely `meow'
    In the expression: runServer "0.0.0.0" 8000 meow
    In an equation for `main': main = runServer "0.0.0.0" 8000 meow
4

1 回答 1

2

第一个错误是因为您没有启用OverloadedStrings语言扩展。没有它, a"xyz"是 a String,但Data.Text.append需要两个Texts 作为参数。添加

{-# LANGUAGE OverloadedStrings #-}

到模块的顶部来修复那个。

第二个错误是因为第三个参数runServer必须有类型

Request -> WebSockets p0 ()

但你给了它一个WebSockets p1 (). 如果你想传递一个动作,你必须将它提升为一个函数,

main = runServer "0.0.0.0" 8000 $ const meow

会编译(它是否会工作[做你想做的]是我无法回答的问题,它只会忽略所有请求并总是做同样的事情)。

于 2013-01-25T12:22:30.820 回答