我想运行一个websocket服务器并将消息从另一个模块发送到它。
到目前为止,我只设法将一个通道传递给启动服务器的模块。但是我希望拥有全局的writeFile
,可以随时从任何模块调用。
我也想有多个客户与sendMessage
.一旦连接关闭,我假设线程仍然停留在forever
循环中。
服务器.hs
import Network.WebSockets
import Control.Concurrent
import Control.Monad
import Data.ByteString
createServer :: IO (Chan ByteString)
createServer = do
chan <- newChan
forkIO $ runServer "127.0.0.1" 8080 (serverApp chan)
ready <- readChan chan -- wait for client
return chan
serverApp :: Chan ByteString -> PendingConnection -> IO ()
serverApp chan pending =
do
print "Client connected"
connection <- acceptRequest pending
writeChan chan "ready"
forever $ do
msg <- readChan chan
sendTextData connection msg
sendMessage :: Chan ByteString -> ByteString -> IO ()
sendMessage = writeChan
主.hs
main :: IO ()
main = do
client <- createServer
sendMessage client ("hello" :: ByteString)
我最终使用了带有unsafePerformIO
的MVar
。虽然不完全推荐,但代码整洁简单。
createServer :: IO ()
createServer = do
_ <- forkIO $ runServer "127.0.0.1" 8080 serverApp
return ()
serverApp :: PendingConnection -> IO ()
serverApp pending =
do
connection <- acceptRequest pending
forever $ do
msg <- takeMVar channel
sendTextData connection msg
channel :: MVar ByteString
{-# NOINLINE channel #-}
channel = unsafePerformIO newEmptyMVar
sendMessage :: ByteString -> IO ()
sendMessage = putMVar channel
代码仍然缺少异常处理,它仅适用于 1 个连接的客户端。