php--
php--

Reputation: 3237

Running websockets Haskell example

Running websockets Haskell example, show below, obviously does not work since there is no main function.

running ghc --make Main.hs -o Main confirms that

Meow function requires websockets connection. How to open the connection?

The library used is https://github.com/jaspervdj/websockets.

{-# LANGUAGE OverloadedStrings #-}
import           Control.Monad      (forever)
import qualified Data.Text          as T
import qualified Network.WebSockets as WS

meow :: WS.Connection -> IO ()
meow conn = forever $ do
    msg <- WS.receiveData conn
    WS.sendTextData conn $ msg `T.append` ", meow"

Upvotes: 1

Views: 733

Answers (1)

Pierre R
Pierre R

Reputation: 216

If you have a look at the example folder:

https://github.com/jaspervdj/websockets/blob/master/example/client.hs

main = withSocketsDo $ WS.runClient "echo.websocket.org" 80 "/" app

app is of type app :: WS.ClientApp () which is a synonym for Connection -> IO a

runClient will open the socket connection for you. If you want to know how, take a look at the source of the function (https://hackage.haskell.org/package/websockets-0.9.3.0/docs/src/Network-WebSockets-Client.html#runClient).

As an aside, withSocketDo belongs to socket. You will find the explanation here: https://hackage.haskell.org/package/network-2.6.0.2/docs/Network-Socket.html#v:withSocketsDo

There are other examples here: http://jaspervdj.be/websockets/example.html

I haven't used websocket and it is usually not such a good idea to answer unfamiliar topic. Hope to be of some help.

Upvotes: 1

Related Questions