on 1238607 help sign in
elk/terminal/src/Develop/Socket.hs
43 lines 1.2 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Develop.Socket (watchFile) where
5import Control.Concurrent (forkIO, threadDelay)
6import Control.Exception (SomeException, catch)
7import qualified Data.ByteString.Char8 as BS
8import qualified Network.WebSockets as WS
9import qualified System.FSNotify.Devel as Notify
10import qualified System.FSNotify as Notify
14watchFile :: FilePath -> WS.PendingConnection -> IO ()
15watchFile watchedFile pendingConnection =
16 do connection <- WS.acceptRequest pendingConnection
18 Notify.withManager $ \mgmt ->
19 do stop <- Notify.treeExtAny mgmt "." ".elm" print
20 tend connection
21 stop
24tend :: WS.Connection -> IO ()
25tend connection =
26 let
27 pinger :: Integer -> IO a
28 pinger n =
29 do threadDelay (5 * 1000 * 1000)
30 WS.sendPing connection (BS.pack (show n))
31 pinger (n + 1)
33 receiver :: IO ()
34 receiver =
35 do _ <- WS.receiveDataMessage connection
36 receiver
38 shutdown :: SomeException -> IO ()
39 shutdown _ =
40 return ()
41 in
42 do _pid <- forkIO (receiver `catch` shutdown)
43 pinger 1 `catch` shutdown