Hook up WS connection to client UI

This commit is contained in:
Joscha 2020-02-10 23:09:13 +00:00
parent 9f5d1c5684
commit 5e7c2952a1
5 changed files with 129 additions and 111 deletions

View file

@ -27,16 +27,13 @@ sendUpdatesThread conn nodeChan _ = do
receivePackets :: TreeModule a => WS.Connection -> a -> IO ()
receivePackets conn treeModule = forever $ do
maybePacket <- receivePacket conn
case maybePacket of
Nothing -> pure ()
Just packet ->
case packet of
ClientEdit path text -> edit treeModule path text
ClientDelete path -> delete treeModule path
ClientReply path text -> reply treeModule path text
ClientAct path -> act treeModule path
ClientHello _ -> closeWithErrorMessage conn "Invalid packet: Hello can only be sent once"
packet <- receivePacket conn
case packet of
ClientEdit path text -> edit treeModule path text
ClientDelete path -> delete treeModule path
ClientReply path text -> reply treeModule path text
ClientAct path -> act treeModule path
ClientHello _ -> closeWithErrorMessage conn "Invalid packet: Hello can only be sent once"
serverApp :: TreeModule a => Int -> ModuleConstructor a -> WS.ServerApp
serverApp pingDelay constructor pendingConnection = do
@ -45,9 +42,8 @@ serverApp pingDelay constructor pendingConnection = do
WS.withPingThread conn pingDelay (pure ()) $ do
firstPacket <- receivePacket conn
case firstPacket of
Nothing -> pure ()
Just (ClientHello _) -> do
ClientHello _ -> do
sendPacket conn $ ServerHello [] initialNode
withThread (sendUpdatesThread conn chan initialNode) $
constructor (writeChan chan) $ receivePackets conn
Just _ -> closeWithErrorMessage conn "Invalid packet: Expected a hello packet"
_ -> closeWithErrorMessage conn "Invalid packet: Expected a hello packet"