ciembor
ciembor

Reputation: 7337

How to run official example of Haskell WebSockets library

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

Upvotes: 0

Views: 437

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183888

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 Texts 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).

Upvotes: 2

Related Questions