1

IRC ネットワークに接続する (ソケットを開く) 必要があり、Web アプリもホストするアプリケーションを構築しています。一部のデータは、IRC パーツと Http パーツの間で共有する必要があります。Haskell でそのような同時実行性を実現する最善の方法は何ですか? Control.Monad.STMこれを実装するために使用する必要がありますか?これは Web フレームワークもサポートすべきものですか、それとも Scotty を開始する前に fork することは可能ですか?

4

1 に答える 1

1

以下の作品は驚くほど素晴らしいものでした。STM.TQueue を使用することになりました。

listen :: SSL.SSL -> IO ()
listen ssl = do
  lines <- BL.lines `fmap` SSL.lazyRead ssl
  mapM_ (processLine ssl . show) lines

processQueue :: SSL.SSL -> TQueue String -> IO ()
processQueue ssl queue = forever $ do newData <- atomically $ readTQueue queue
                                      privmsg ssl newData

web_service :: TQueue String -> IO ()
web_service queue = scotty 25000 $ do
  get "/" $ do
    liftIO $ atomically $ writeTQueue queue "test passed"    
    status ok200

irc_bot :: [AddrInfo] -> TQueue String -> IO ()
irc_bot addrs queue = do ssl <- connectToServer (head addrs)
                         forkIO $ listen ssl
                         processQueue ssl queue

main = withOpenSSL $ do
  let hints = defaultHints { addrSocketType = Stream, addrFamily = AF_INET }
  addrs <- getAddrInfo (Just hints) (Just "irc.freenode.com") (Just "6697")
  queue <- atomically newTQueue
  forkIO $ irc_bot addrs queue
  web_service queue
于 2015-03-18T18:08:47.460 に答える