Skip to content

Commit

Permalink
WebSocketChat backend route and server fix. working!
Browse files Browse the repository at this point in the history
  • Loading branch information
dfordivam committed Nov 13, 2018
1 parent 4d579e2 commit db4673f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions backend/backend.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ library
, frontend
, obelisk-backend
, obelisk-route
, websockets-snap
exposed-modules:
Backend
ghc-options: -Wall
Expand Down
14 changes: 13 additions & 1 deletion backend/src/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ module Backend where

import Common.Route
import Obelisk.Backend
import Data.Dependent.Sum (DSum (..))
import Data.Functor.Identity
import Control.Concurrent
import Network.WebSockets.Snap

import qualified Backend.Examples.WebSocketChat.Server as WebSocketChat

backend :: Backend BackendRoute FrontendRoute
backend = Backend
{ _backend_run = \serve -> serve $ const $ return ()
{ _backend_run = \serve -> do
state <- newMVar WebSocketChat.newServerState
serve $ \case
BackendRoute_Missing :=> Identity () -> return ()
BackendRoute_WebSocketChat :=> Identity () -> do
runWebSocketsSnap (WebSocketChat.application state)

, _backend_routeEncoder = backendRouteEncoder
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}

module Main where
module Backend.Examples.WebSocketChat.Server where

import Control.Concurrent (MVar, modifyMVar, modifyMVar_, newMVar,
readMVar)
Expand All @@ -17,7 +17,7 @@ import qualified Data.Text.IO as T
import qualified Network.WebSockets as WS

--------------------------------------------------------------------------------
import CommonWsChat
import Common.Examples.WebSocketChat.Message
--------------------------------------------------------------------------------

type Client = (Text, WS.Connection)
Expand Down Expand Up @@ -46,11 +46,6 @@ broadcast message clients = do
forM_ clients $ \(_, conn) -> WS.sendTextData conn $
(toStrict . encode . S2Cbroadcast) message

main :: IO ()
main = do
state <- newMVar newServerState
WS.runServer "127.0.0.1" 8000 $ application state

application :: MVar ServerState -> WS.ServerApp
application state pending = do
conn <- WS.acceptRequest pending
Expand Down
2 changes: 2 additions & 0 deletions common/src/Common/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Obelisk.Route.TH
data BackendRoute :: * -> * where
-- | Used to handle unparseable routes.
BackendRoute_Missing :: BackendRoute ()
BackendRoute_WebSocketChat :: BackendRoute ()
-- You can define any routes that will be handled specially by the backend here.
-- i.e. These do not serve the frontend, but do something different, such as serving static files.

Expand All @@ -57,6 +58,7 @@ backendRouteEncoder = handleEncoder (const (InL BackendRoute_Missing :/ ())) $
pathComponentEncoder $ \case
InL backendRoute -> case backendRoute of
BackendRoute_Missing -> PathSegment "missing" $ unitEncoder mempty
BackendRoute_WebSocketChat -> PathSegment "websocketchat" $ unitEncoder mempty
InR obeliskRoute -> obeliskRouteSegment obeliskRoute $ \case
-- The encoder given to PathEnd determines how to parse query parameters,
-- in this example, we have none, so we insist on it.
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Frontend/Examples/WebSocketChat/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ app = do
loggedInEv = fmapMaybe loginEv msgRecEv
wsRespEv <- prerender (return never) $ do
let sendEv = fmap ((:[]) . toStrict . encode) msgSendEv
ws <- webSocket "ws://localhost:8000" $ def & webSocketConfig_send .~ sendEv
ws <- webSocket "ws://localhost:8000/websocketchat" $ def & webSocketConfig_send .~ sendEv
return (_webSocket_recv ws)
receivedMessages <- foldDyn (\m ms -> ms ++ [m]) [] eRecRespTxt
el "div" $ do
Expand Down

0 comments on commit db4673f

Please sign in to comment.