2023-04-01 17:43:09 +02:00
|
|
|
module WebSocket.WSApp (WSEnv (..), wsApp, WSApp (..)) where
|
2023-01-27 18:34:28 +01:00
|
|
|
|
2023-02-19 11:41:32 +01:00
|
|
|
import BroadcastUserData
|
|
|
|
( MonadBroadcast (..),
|
|
|
|
broadcastUserData,
|
2023-02-07 20:30:25 +01:00
|
|
|
)
|
2023-02-19 11:41:32 +01:00
|
|
|
import ClassyPrelude
|
|
|
|
import Data.UUID.V4 ()
|
|
|
|
import State.ConnectedClientsState (MonadConnectedClientsModify (..))
|
|
|
|
import State.RoomDataState (MonadRoomDataStateRead (..))
|
|
|
|
import Types.ConnectionState (Client (..))
|
2023-02-18 16:57:20 +01:00
|
|
|
import Types.WebSocketMessages.WebSocketMessages
|
|
|
|
( SetClientInfo (displayName),
|
|
|
|
WebSocketMessage (..),
|
|
|
|
)
|
2023-04-08 15:57:33 +02:00
|
|
|
import WebSocket.AllChat (broadCastAllChatMessage)
|
2023-02-19 11:41:32 +01:00
|
|
|
import WebSocket.MonadWebSocketSession
|
|
|
|
import WebSocket.WSReaderTApp
|
2023-02-18 18:25:38 +01:00
|
|
|
|
2023-01-27 18:34:28 +01:00
|
|
|
wsApp ::
|
2023-02-19 11:41:32 +01:00
|
|
|
( MonadWebSocketSession m,
|
|
|
|
MonadWebSocketSessionInit m,
|
|
|
|
MonadConnectedClientsModify m,
|
|
|
|
MonadRoomDataStateRead m,
|
|
|
|
MonadBroadcast m
|
2023-01-27 18:34:28 +01:00
|
|
|
) =>
|
|
|
|
m ()
|
|
|
|
wsApp = do
|
2023-04-01 17:43:09 +02:00
|
|
|
clientInfo <- getTypedWSMessage
|
|
|
|
client <- newClient clientInfo
|
2023-01-27 19:53:34 +01:00
|
|
|
addWSClient client
|
2023-02-07 20:30:25 +01:00
|
|
|
broadcastUserData
|
2023-02-19 11:41:32 +01:00
|
|
|
withCleanUp $ forever $ do
|
2023-02-18 16:57:20 +01:00
|
|
|
handleWSAction
|
2023-02-07 20:30:25 +01:00
|
|
|
broadcastUserData
|
2023-02-18 16:57:20 +01:00
|
|
|
|
|
|
|
handleWSAction ::
|
2023-02-19 11:41:32 +01:00
|
|
|
( MonadWebSocketSession m,
|
2023-04-08 15:57:33 +02:00
|
|
|
MonadConnectedClientsModify m,
|
|
|
|
MonadBroadcast m
|
2023-02-18 16:57:20 +01:00
|
|
|
) =>
|
|
|
|
m ()
|
|
|
|
handleWSAction = do
|
|
|
|
msg <- getTypedWSMessage
|
|
|
|
case msg of
|
|
|
|
JoinRoomMessage _ -> do
|
|
|
|
joinRoom
|
|
|
|
ClientInfoMessage clientInfo -> do
|
|
|
|
updateClientName clientInfo
|
2023-04-08 15:57:33 +02:00
|
|
|
AllChatMessageIncomingMessage incomingMessage -> do
|
|
|
|
broadCastAllChatMessage incomingMessage
|
2023-01-27 18:34:28 +01:00
|
|
|
|
2023-02-19 11:41:32 +01:00
|
|
|
joinRoom ::
|
|
|
|
( MonadConnectedClientsModify m,
|
|
|
|
MonadWebSocketSession m
|
2023-01-27 18:34:28 +01:00
|
|
|
) =>
|
2023-02-19 11:41:32 +01:00
|
|
|
m ()
|
|
|
|
joinRoom = do
|
|
|
|
clientId <- getSesssionId
|
|
|
|
updateWSClient clientId (\c -> c {joinedRoom = True})
|
2023-02-18 16:57:20 +01:00
|
|
|
|
2023-02-19 11:41:32 +01:00
|
|
|
updateClientName ::
|
|
|
|
( MonadWebSocketSession m,
|
|
|
|
MonadConnectedClientsModify m
|
2023-02-18 16:57:20 +01:00
|
|
|
) =>
|
2023-02-19 11:41:32 +01:00
|
|
|
SetClientInfo ->
|
2023-02-18 16:57:20 +01:00
|
|
|
m ()
|
2023-02-19 11:41:32 +01:00
|
|
|
updateClientName clientInfo = do
|
|
|
|
clientId <- getSesssionId
|
|
|
|
updateWSClient clientId (\c -> c {name = displayName clientInfo})
|