on wai-handler-launch/1.3.1.5 help sign in
153 lines 5.5 kB history raw
1websockets example
2==================
4This is the Haskell implementation of the example for the WebSockets library. We
5implement a simple multi-user chat program. A live demo of the example is
6available [here](http://jaspervdj.be/websockets-example). In order to understand
7this example, keep the [reference](http://jaspervdj.be/websockets/reference)
8nearby to check out the functions we use.
10> {-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
11> import Data.Char (isPunctuation, isSpace)
12> import Data.Monoid (mappend)
13> import Data.Text (Text)
14> import Control.Exception (fromException, handle)
15> import Control.Monad (forM_, forever)
16> import Control.Concurrent (MVar, newMVar, modifyMVar_, readMVar)
17> import Control.Monad.IO.Class (liftIO)
18> import qualified Data.Text as T
19> import qualified Data.Text.IO as T
21> import qualified Network.WebSockets as WS
22> import qualified Network.Wai
23> import qualified Network.Wai.Handler.Warp as Warp
24> import qualified Network.Wai.Handler.WebSockets as WaiWS
25> import qualified Network.Wai.Application.Static as Static
26> import Data.FileEmbed (embedDir)
28We represent a client by his username and a 'WS.Connection'. We will see how we obtain this 'WS.Connection' later on.
30> type Client = (Text, WS.Connection)
32The state kept on the server is simply a list of connected clients. We've added
33an alias and some utility functions, so it will be easier to extend this state
34later on.
36> type ServerState = [Client]
38Create a new, initial state
40> newServerState :: ServerState
41> newServerState = []
43Get the number of active clients
45> numClients :: ServerState -> Int
46> numClients = length
48Check if a user already exists (based on username)
50> clientExists :: Client -> ServerState -> Bool
51> clientExists client = any ((== fst client) . fst)
53Add a client (first, you should verify the client is not already connected using
54'clientExists')
56> addClient :: Client -> ServerState -> ServerState
57> addClient client clients = client : clients
59Remove a client
61> removeClient :: Client -> ServerState -> ServerState
62> removeClient client = filter ((/= fst client) . fst)
64Send a message to all clients, and log it on stdout.
66> broadcast :: Text -> ServerState -> IO ()
67> broadcast message clients = do
68> T.putStrLn message
69> forM_ clients $ \(_, conn) -> WS.sendTextData conn message
71The main function first creates a new state for the server, then spawns the
72actual server. For this purpose, we use the simple server provided by
73'WS.runServer'.
75> main :: IO ()
76> main = do
77> putStrLn "http://localhost:9160/client.html"
78> state <- newMVar newServerState
79> Warp.runSettings Warp.defaultSettings
80> { Warp.settingsPort = 9160
81> , Warp.settingsIntercept = WaiWS.intercept (application state)
82> } staticApp
84> staticApp :: Network.Wai.Application
85> staticApp = Static.staticApp $ Static.embeddedSettings $(embedDir "static")
87When a client connects, we accept the connection, regardless of the path.
89> application :: MVar ServerState -> WS.ServerApp
90> application state pending = do
91> conn <- WS.acceptRequest pending
93When a client is succesfully connected, we read the first message. This should
94be in the format of "Hi, I am Jasper", where Jasper is the requested username.
96> msg <- WS.receiveData conn
97> clients <- liftIO $ readMVar state
98> case msg of
100Check that the first message has the right format
102> _ | not (prefix `T.isPrefixOf` msg) ->
103> WS.sendTextData conn ("Wrong announcement" :: Text)
105Check the validity of the username
107> | any ($ fst client)
108> [T.null, T.any isPunctuation, T.any isSpace] ->
109> WS.sendTextData conn ("Name cannot " `mappend`
110> "contain punctuation or whitespace, and " `mappend`
111> "cannot be empty" :: Text)
113Check that the given username is not already taken
115> | clientExists client clients ->
116> WS.sendTextData conn ("User already exists" :: Text)
118All is right!
120> | otherwise -> do
122We send a "Welcome!", according to our own little protocol. We add the client to
123the list and broadcast the fact that he has joined. Then, we give control to the
124'talk' function.
126> liftIO $ modifyMVar_ state $ \s -> do
127> let s' = addClient client s
128> WS.sendTextData conn $
129> "Welcome! Users: " `mappend`
130> T.intercalate ", " (map fst s)
131> broadcast (fst client `mappend` " joined") s'
132> return s'
133> talk conn state client
134> where
135> prefix = "Hi! I am "
136> client = (T.drop (T.length prefix) msg, conn)
138The talk function continues to read messages from a single client until he
139disconnects. All messages are broadcasted to the other clients.
141> talk :: WS.Connection -> MVar ServerState -> Client -> IO ()
142> talk conn state client@(user, _) = handle catchDisconnect $
143> forever $ do
144> msg <- WS.receiveData conn
145> liftIO $ readMVar state >>= broadcast
146> (user `mappend` ": " `mappend` msg)
147> where
148> catchDisconnect e = case fromException e of
149> Just WS.ConnectionClosed -> liftIO $ modifyMVar_ state $ \s -> do
150> let s' = removeClient client s
151> broadcast (user `mappend` " disconnected") s'
152> return s'
153> _ -> return ()