2016-11-21 12 views
0

クライアントにリアルタイムの通知を送信するために、Redis pub-subでwai-websocketsを使用する例をどこから見つけることができますか?WebsocketsとRedis pubsub

wai-websocketshedisのhacakgeのマニュアルを読んだことがありますが、希望の結果を得るために2つの方法を組み合わせる方法はまだ分かりません。

答えて

0

誰かがこの投稿に不思議に思っている場合は、最近私は基本的にhediswebsocketsアプリを実装しました。シンプルな実装は、スレッド内のhedisサブスクライバと別のスレッド内のwebsocketで実行され、TChan経由で通信されます。

いくつかのサンプルコード

main = do 
    chan <- atomically $ newTChan 
    a <- forkIO (subscribeProc chan) 
    b <- forkIO (webSock chan) 
    -- wait for a and b 

-- Hedis subscriber 
subscribeProc :: TChan B.ByteString -> IO() 
subscribeProc chan = do 
    conn <- connect defaultConnectInfo 
    pubSubCtrl <- newPubSubController [("redis-channel", chanHandler chan)] [] 
    forever $ 
    pubSubForever conn pubSubCtrl onInitialComplete 
    `catch` (\(e :: SomeException) -> do 
       Prelude.putStrLn $ "Got error: " ++ show e 
       threadDelay $ 60*1000) 


chanHandler :: TChan B.ByteString -> B.ByteString -> IO() 
chanHandler chan msg = 
    atomically $ writeTChan chan (msg) 

onInitialComplete :: IO() 
onInitialComplete = putStrLn 
    "Redis acknowledged that mychannel is now subscribed" 


-- websocket 
webSock :: TChan B.ByteString -> IO() 
webSock chan = do 
    WS.runServer appHost appPort $ handleConnection chan 

handleConnection :: TChan B.ByteString -> WS.PendingConnection -> IO() 
handleConnection chan pending = do 
    connection <- WS.acceptRequest pending 
    loop chan connection 
    where 
    loop chan connection = do 
     msg <- atomically $ readTChan chan 
     putStrLn $ "got data " ++ show msg 
     WS.sendTextData connection msg 
     loop chan connection 
関連する問題