on 52182bc help sign in
33 lines 1.4 kB history raw
1{-# LANGUAGE CPP #-}
2import ProjectM36.Server.WebSocket
3import ProjectM36.Server.Config
4import ProjectM36.Server.ParseArgs
5import ProjectM36.Server
6import Control.Concurrent
7import qualified Network.WebSockets as WS
9#if MIN_VERSION_network_transport_tcp(0,6,0)
10import Network.Transport.TCP.Internal (decodeEndPointAddress)
11#else
12import Network.Transport.TCP (decodeEndPointAddress)
13#endif
15import Control.Exception
17main :: IO ()
18main = do
19 -- launch normal project-m36-server
20 addressMVar <- newEmptyMVar
21 serverConfig <- parseConfigWithDefaults (defaultServerConfig { bindPort = 8000, bindHost = "127.0.0.1" })
22 --usurp the serverConfig for our websocket server and make the proxied server run locally
23 let wsHost = bindHost serverConfig
24 wsPort = bindPort serverConfig
25 serverHost = "127.0.0.1"
26 serverConfig' = serverConfig {bindPort = 0, bindHost = serverHost}
27 _ <- forkFinally (launchServer serverConfig' (Just addressMVar) >> pure ()) (either throwIO pure)
28 --wait for server to be listening
29 address <- takeMVar addressMVar
30 let Just (_, serverPort, _) = decodeEndPointAddress address
31 --this built-in server is apparently not meant for production use, but it's easier to test than starting up the wai or snap interfaces
32 WS.runServer wsHost (fromIntegral wsPort) (websocketProxyServer (read serverPort) serverHost)