如何运行Haskell WebSockets库的官方示例 [英] How to run official example of Haskell WebSockets library
问题描述
import Data.Char(isPunctuation,isSpace)
导入Data.Monoid(mappend)
导入Data.Text(文本)
导入Control.Exception(fromException)
导入Control.Monad(forM_,永远)
导入控制。并发(MVar,newMVar,modifyMVar_,readMVar)
导入Control.Monad.IO.Class(liftIO)
将合格的Data.Text导入为T
将合格的Data.Text.IO导入为T
import Network.WebSockets
meow :: TextProtocol p => WebSockets p()
meow = forever $ do
msg< - receiveData
sendTextData $ msg`T.append`,meow。
main :: IO()
main = runServer0.0.0.08000 meow
我得到:
ciembor @ freedom〜> ghc hchat.hs;和./hchat
[1的1]编译主(hchat.hs,hchat.o)
hchat.hs:15:35:
无法匹配预期类型'Text'的实际类型为'[Char]'
'T.append'的第二个参数,即`,meow。'
在'($)'的第二个参数中,即'msg`T.append`,meow。'
在'do'块的标记中:sendTextData $ msg`T.append`,喵。
hchat.hs:18:33:
无法匹配预期的类型`Request - > WebSockets p0()'
实际类型为`WebSockets p1()'
在`runServer'的第三个参数中,即`meow'
在表达式中:runServer0.0.0.08000 meow
在`main'等式中:main = runServer0.0.0.08000 meow
xyz
是 String
,但 Data.Text.append
将两个 Text
s作为参数。添加 { - #LANGUAGE OverloadedStrings# - }
到模块的顶部来修复这个问题。
第二个错误是因为第三个参数 runServer
必须有类型 请求 - > WebSockets p0()
但是您给它一个 WebSockets p1() code>。如果你想传递一个动作,你必须将它提升到一个函数,
main = runServer0.0.0.08000 $ const meow
会编译(不管它会工作[做你想做的事]是一个我不能解决的问题答案,那会忽略所有请求,并且总是做同样的事情)。
According to this topic: https://stackoverflow.com/questions/14494922/examples-of-websocket-usage-in-haskell I have my first question. Why official example of WebSockets library doesn't run on my machine?
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
I get:
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
The first error is because you didn't enable the OverloadedStrings
language extension. Without that, a "xyz"
is a String
, but Data.Text.append
takes two Text
s as arguments. Add
{-# LANGUAGE OverloadedStrings #-}
to the top of the module to fix that one.
The second error is because the third argument to runServer
must have type
Request -> WebSockets p0 ()
but you gave it a WebSockets p1 ()
. If you want to pass an action, you have to lift it to a function,
main = runServer "0.0.0.0" 8000 $ const meow
would compile (whether it would work [do what you want] is a question I cannot answer, that would just ignore all requests and always do the same thing).
这篇关于如何运行Haskell WebSockets库的官方示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!