{-# LANGUAGE OverloadedStrings #-}
module Box.Websocket.Example where
import Box
import Box.Socket.Types
import Box.Websocket
import Control.Concurrent.Async
import Data.Functor.Contravariant
import Data.Text (Text)
senderExample :: [Text] -> IO [Text]
senderExample :: [Text] -> IO [Text]
senderExample [Text]
ts = do
(Committer IO Text
c, IO [Text]
r) <- IO (Committer IO Text, IO [Text])
forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <- IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async (SocketConfig -> PostSend -> Box IO Text Text -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> PostSend -> Box IO a a -> IO ()
serverBox SocketConfig
defaultSocketConfig (Double -> PostSend
CloseAfter Double
0.2) (Box IO Text Text -> IO ())
-> (Emitter IO Text -> Box IO Text Text)
-> Emitter IO Text
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Committer IO Text -> Emitter IO Text -> Box IO Text Text
forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO Text
forall a. Monoid a => a
mempty (Emitter IO Text -> IO ())
-> Codensity IO (Emitter IO Text) -> IO ()
forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> [Text] -> Codensity IO (Emitter IO Text)
forall a. [a] -> CoEmitter IO a
qList [Text]
ts)
Double -> IO ()
sleep Double
0.1
SocketConfig -> PostSend -> Box IO Text Text -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> PostSend -> Box IO a a -> IO ()
clientBox SocketConfig
defaultSocketConfig PostSend
StayOpen (Committer IO Text -> Emitter IO Text -> Box IO Text Text
forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO Text
c Emitter IO Text
forall a. Monoid a => a
mempty)
Double -> IO ()
sleep Double
0.1
Async () -> IO ()
forall a. Async a -> IO ()
cancel Async ()
a
IO [Text]
r
echoExample :: [Text] -> IO [Text]
echoExample :: [Text] -> IO [Text]
echoExample [Text]
ts = do
(Committer IO Text
c, IO [Text]
r) <- IO (Committer IO Text, IO [Text])
forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <-
IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async
(SocketConfig -> (Text -> Maybe Text) -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> (a -> Maybe a) -> IO ()
responseServer SocketConfig
defaultSocketConfig (Text -> Maybe Text
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text
"echo: " :: Text) <>)))
Double -> IO ()
sleep Double
0.1
SocketConfig -> PostSend -> Box IO Text Text -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> PostSend -> Box IO a a -> IO ()
clientBox SocketConfig
defaultSocketConfig (Double -> PostSend
CloseAfter Double
0.2) (Box IO Text Text -> IO ())
-> (Emitter IO Text -> Box IO Text Text)
-> Emitter IO Text
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Committer IO Text -> Emitter IO Text -> Box IO Text Text
forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO Text
c (Emitter IO Text -> IO ())
-> Codensity IO (Emitter IO Text) -> IO ()
forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> [Text] -> Codensity IO (Emitter IO Text)
forall a. [a] -> CoEmitter IO a
qList [Text]
ts
Double -> IO ()
sleep Double
0.1
Async () -> IO ()
forall a. Async a -> IO ()
cancel Async ()
a
IO [Text]
r
echoLogExample :: [Text] -> IO ([Text], [Text])
echoLogExample :: [Text] -> IO ([Text], [Text])
echoLogExample [Text]
ts = do
(Committer IO Text
c, IO [Text]
r) <- IO (Committer IO Text, IO [Text])
forall a. IO (Committer IO a, IO [a])
refCommitter
(Committer IO Text
cLog, IO [Text]
resLog) <- IO (Committer IO Text, IO [Text])
forall a. IO (Committer IO a, IO [a])
refCommitter
Async ()
a <-
IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async
((Text -> IO (Maybe Text)) -> Box IO Text Text -> IO ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Box m b a -> m ()
fuse (Maybe Text -> IO (Maybe Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Text -> IO (Maybe Text))
-> (Text -> Maybe Text) -> Text -> IO (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe Text
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe Text) -> (Text -> Text) -> Text -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text
"echo: " :: Text) <>)) (Box IO Text Text -> IO ())
-> Codensity IO (Box IO Text Text) -> IO ()
forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> (Box IO Text Text -> IO ()) -> Codensity IO (Box IO Text Text)
forall a b r. (Box IO a b -> IO r) -> CoBox IO b a
fromAction (\Box IO Text Text
b -> PostSend
-> Committer IO Text -> Box IO Text Text -> Connection -> IO ()
forall a.
(WebSocketsData a, Show a) =>
PostSend -> Committer IO Text -> Box IO a a -> Connection -> IO ()
duplex_ (Double -> PostSend
CloseAfter Double
0.5) ((Text -> Text) -> Committer IO Text -> Committer IO Text
forall a' a. (a' -> a) -> Committer IO a -> Committer IO a'
forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap (Text
"server:" <>) Committer IO Text
cLog) Box IO Text Text
b (Connection -> IO ()) -> Codensity IO Connection -> IO ()
forall {k} a (m :: k -> *) (r :: k).
(a -> m r) -> Codensity m a -> m r
<$|> SocketConfig -> Codensity IO Connection
serve SocketConfig
defaultSocketConfig))
Double -> IO ()
sleep Double
0.1
PostSend
-> Committer IO Text -> Box IO Text Text -> Connection -> IO ()
forall a.
(WebSocketsData a, Show a) =>
PostSend -> Committer IO Text -> Box IO a a -> Connection -> IO ()
duplex_ (Double -> PostSend
CloseAfter Double
0.2) ((Text -> Text) -> Committer IO Text -> Committer IO Text
forall a' a. (a' -> a) -> Committer IO a -> Committer IO a'
forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap (Text
"client:" <>) Committer IO Text
cLog) (Box IO Text Text -> Connection -> IO ())
-> (Emitter IO Text -> Box IO Text Text)
-> Emitter IO Text
-> Connection
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Committer IO Text -> Emitter IO Text -> Box IO Text Text
forall (m :: * -> *) c e. Committer m c -> Emitter m e -> Box m c e
Box Committer IO Text
c (Emitter IO Text -> Connection -> IO ())
-> Codensity IO (Emitter IO Text)
-> Codensity IO (Connection -> IO ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> Codensity IO (Emitter IO Text)
forall a. [a] -> CoEmitter IO a
qList [Text]
ts Codensity IO (Connection -> IO ())
-> Codensity IO Connection -> IO ()
forall {k} (m :: k -> *) a (r :: k).
Codensity m (a -> m r) -> Codensity m a -> m r
<*|> SocketConfig -> Codensity IO Connection
connect SocketConfig
defaultSocketConfig
Double -> IO ()
sleep Double
0.1
Async () -> IO ()
forall a. Async a -> IO ()
cancel Async ()
a
(,) ([Text] -> [Text] -> ([Text], [Text]))
-> IO [Text] -> IO ([Text] -> ([Text], [Text]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Text]
r IO ([Text] -> ([Text], [Text])) -> IO [Text] -> IO ([Text], [Text])
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO [Text]
resLog
clientIO :: IO ()
clientIO :: IO ()
clientIO =
SocketConfig -> PostSend -> Box IO Text Text -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> PostSend -> Box IO a a -> IO ()
clientBox SocketConfig
defaultSocketConfig (Double -> PostSend
CloseAfter Double
0) (Text -> Box IO Text Text
stdBox Text
"q")
serverIO :: IO ()
serverIO :: IO ()
serverIO = SocketConfig -> PostSend -> Box IO Text Text -> IO ()
forall a.
WebSocketsData a =>
SocketConfig -> PostSend -> Box IO a a -> IO ()
serverBox SocketConfig
defaultSocketConfig (Double -> PostSend
CloseAfter Double
0) (Text -> Box IO Text Text
stdBox Text
"q")