chat-with-timeout-control.hs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. {-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings #-}
  2. import Yesod.Core
  3. import Yesod.WebSockets
  4. import qualified Data.Text.Lazy as TL
  5. import Control.Monad (forever)
  6. import Control.Monad.Trans.Reader
  7. import Control.Concurrent (threadDelay)
  8. import Data.Time
  9. import Conduit
  10. import Data.Monoid ((<>))
  11. import Control.Concurrent.STM.Lifted
  12. import Data.Text (Text)
  13. data App = App (TChan Text)
  14. instance Yesod App
  15. mkYesod "App" [parseRoutes|
  16. / HomeR GET
  17. |]
  18. chatApp :: WebSocketsT Handler ()
  19. chatApp = do
  20. sendTextData ("Welcome to the chat server, please enter your name." :: Text)
  21. name <- receiveData
  22. sendTextData $ "Welcome, " <> name
  23. App writeChan <- getYesod
  24. readChan <- atomically $ do
  25. writeTChan writeChan $ name <> " has joined the chat"
  26. dupTChan writeChan
  27. race_
  28. (forever $ atomically (readTChan readChan) >>= sendTextData)
  29. (sourceWS $$ mapM_C (\msg ->
  30. atomically $ writeTChan writeChan $ name <> ": " <> msg))
  31. getHomeR :: Handler Html
  32. getHomeR = do
  33. webSockets chatApp
  34. defaultLayout $ do
  35. [whamlet|
  36. <div #output>
  37. <form #form>
  38. <input #input autofocus>
  39. |]
  40. toWidget [lucius|
  41. \#output {
  42. width: 600px;
  43. height: 400px;
  44. border: 1px solid black;
  45. margin-bottom: 1em;
  46. p {
  47. margin: 0 0 0.5em 0;
  48. padding: 0 0 0.5em 0;
  49. border-bottom: 1px dashed #99aa99;
  50. }
  51. }
  52. \#input {
  53. width: 600px;
  54. display: block;
  55. }
  56. |]
  57. toWidget [julius|
  58. var url = document.URL,
  59. output = document.getElementById("output"),
  60. form = document.getElementById("form"),
  61. input = document.getElementById("input"),
  62. conn;
  63. url = url.replace("http:", "ws:").replace("https:", "wss:");
  64. conn = new WebSocket(url);
  65. conn.onmessage = function(e) {
  66. var p = document.createElement("p");
  67. p.appendChild(document.createTextNode(e.data));
  68. output.appendChild(p);
  69. };
  70. /* *******************************************************************************************************
  71. The following code demonstrates one way to prevent timeouts. The "if" test is added to prevent chat participants from getting the ping message “dcba” every twenty seconds. It also prevents participants from receiving any message ending with “dcba” sent by any chat participant. “ e.data.split("").reverse().join("").substring(0,4)” changes, for example, “user:abc123dcba” to “abcd321cba:resu” and grabs the first four characters; i.e., “abcd”. Messages are broadcast only if the last four characters are not “dcba”. Note that the variable "t" controls the length of the timeout period. t = 3 allows one minute of inactivity. t = 30 allows ten minutes, and t = 180 allows an hour. The value inserted below is 360 (2 hours).
  72. */
  73. conn.onmessage = function(e) {
  74. var p = document.createElement("p");
  75. p.appendChild(document.createTextNode(e.data));
  76. if (e.data.split("").reverse().join("").substring(0,4) != "abcd") {
  77. output.appendChild(p);
  78. }
  79. };
  80. var t = 360
  81. setInterval (function () {
  82. t = t - 1;
  83. if (t > 0)
  84. {
  85. conn.send("dcba");
  86. }
  87. }, 20000);
  88. /* ****************************************************************************************************** */
  89. form.addEventListener("submit", function(e){
  90. conn.send(input.value);
  91. input.value = "";
  92. e.preventDefault();
  93. });
  94. |]
  95. main :: IO ()
  96. main = do
  97. chan <- atomically newBroadcastTChan
  98. warp 3000 $ App chan