根据这个主题: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
答案 0 :(得分:2)
第一个错误是因为您未启用OverloadedStrings
语言扩展。没有它,"xyz"
是String
,但Data.Text.append
需要两个Text
作为参数。添加
{-# LANGUAGE OverloadedStrings #-}
到模块的顶部来修复那个。
第二个错误是因为runServer
的第三个参数必须具有类型
Request -> WebSockets p0 ()
但你给了它一个WebSockets p1 ()
。如果要传递操作,则必须将其提升为函数
main = runServer "0.0.0.0" 8000 $ const meow
会编译(是否会工作[做你想要的]是一个我无法回答的问题,这会忽略所有请求并且总是做同样的事情。)