Last active
November 4, 2015 11:21
-
-
Save teh/6ef988c0c9e86bf056db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Concurrent (forkIO) | |
import Control.Monad (forever) | |
import Network.Wai.Handler.Warp (run) | |
import Network.Wai.Handler.WebSockets as WaiWS | |
import Network.WebSockets (acceptRequest, receiveDataMessage, sendTextData, PendingConnection, defaultConnectionOptions, DataMessage(..)) | |
import Data.ByteString.Lazy (ByteString) | |
import Control.Concurrent.Chan.Unagi (InChan, newChan, readChan, dupChan, writeChan) | |
handleWS :: InChan ByteString -> PendingConnection -> IO () | |
handleWS bcast pending = do | |
localChan <- dupChan bcast | |
connection <- acceptRequest pending | |
forkIO $ forever $ do | |
message <- readChan localChan | |
sendTextData connection message | |
-- loop forever | |
let loop = do | |
Text message <- receiveDataMessage connection | |
writeChan bcast message | |
loop | |
loop | |
main :: IO () | |
main = do | |
(bcast, _) <- newChan | |
run 8080 (WaiWS.websocketsOr defaultConnectionOptions (handleWS bcast) undefined) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment