Skip to content

Commit 2f1c5c1

Browse files
committed
make payload Data.Aeson.Value
2 parents f58352e + b2cffd1 commit 2f1c5c1

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

Web/SocketIO/Event.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import Web.SocketIO.Types
1010
--------------------------------------------------------------------------------
1111
import Control.Applicative ((<$>))
1212
import Control.Monad.Reader
13+
import qualified Data.Aeson as Aeson
1314
import qualified Data.ByteString.Lazy as BL
1415

1516
{-# DEPRECATED reply "use msg instead" #-}
1617
--------------------------------------------------------------------------------
1718
-- | This function is deprecated; use 'msg' instead
18-
reply :: CallbackM [Text]
19+
reply :: CallbackM [Aeson.Value]
1920
reply = do
2021
Payload p <- callbackEnvPayload <$> ask
2122
return p
@@ -29,7 +30,7 @@ reply = do
2930
-- liftIO $ print payload
3031
-- emit "echo" payload
3132
-- @
32-
msg :: CallbackM [Text]
33+
msg :: CallbackM [Aeson.Value]
3334
msg = do
3435
Payload p <- callbackEnvPayload <$> ask
3536
return p

Web/SocketIO/Session.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ handleSession SessionHandshake = do
3030
heartbeatTimeout'
3131
(closeTimeout configuration)
3232
(transports configuration)
33-
3433

3534
handleSession SessionConnect = do
36-
logWithSession Info $ "Connected"
35+
debugSession Info $ "Connected"
36+
triggerEvent (Event "connection" (Payload []))
3737
return $ MsgConnect NoEndpoint
3838

3939
handleSession SessionPolling = do

Web/SocketIO/Types/Base.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Control.Concurrent.Chan.Lifted
1919
import Control.Monad.Reader
2020
import Control.Monad.Writer
2121
import Control.Monad.Base
22+
import qualified Data.Aeson as Aeson
2223
import qualified Data.HashMap.Strict as H
2324
import Data.IORef.Lifted
2425
import Network.HTTP.Types.Header (ResponseHeaders)
@@ -153,7 +154,7 @@ class Publisher m where
153154
-- `emit` \"launch\" [\"missile\", \"nuke\"]
154155
-- @
155156
emit :: EventName -- ^ name of event to trigger
156-
-> [Text] -- ^ payload to carry with
157+
-> [Aeson.Value] -- ^ payload to carry with
157158
-> m ()
158159

159160
-- | Sends a message to everybody except for the socket that starts it.
@@ -162,7 +163,7 @@ class Publisher m where
162163
-- `broadcast` \"hide\" [\"nukes coming!\"]
163164
-- @
164165
broadcast :: EventName -- ^ name of event to trigger
165-
-> [Text] -- ^ payload to carry with
166+
-> [Aeson.Value] -- ^ payload to carry with
166167
-> m ()
167168

168169
-- |

Web/SocketIO/Types/Event.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import Web.SocketIO.Types.String
1818

1919
----------------------------------------------------------------------------------
2020
import Control.Applicative
21-
import Data.Aeson
22-
import Data.Aeson.Encode (encodeToTextBuilder)
23-
import Data.List (intersperse)
24-
import Data.Text.Internal.Builder (toLazyText)
21+
import Data.Aeson as Aeson
2522
import qualified Data.Text.Lazy as TL
2623
import Data.Vector (toList)
2724
--------------------------------------------------------------------------------
@@ -30,10 +27,10 @@ type EventName = Text
3027

3128
--------------------------------------------------------------------------------
3229
-- | Payload carried by an Event
33-
data Payload = Payload [Text] deriving (Eq, Show)
30+
data Payload = Payload [Aeson.Value] deriving (Eq, Show)
3431

3532
instance Serializable Payload where
36-
serialize (Payload payload) = serialize $ '[' `TL.cons` (TL.concat $ intersperse "," payload) `TL.snoc` ']'
33+
serialize (Payload payload) = serialize $ Aeson.encode payload
3734

3835
--------------------------------------------------------------------------------
3936
-- | Event
@@ -52,7 +49,7 @@ instance FromJSON Event where
5249
(toArgumentList <$> v .:? "args")
5350
where toArgumentList :: Maybe Value -> Payload
5451
toArgumentList Nothing = Payload []
55-
toArgumentList (Just (Array a)) = Payload $ filter (/= "null") . map (toLazyText . encodeToTextBuilder) . toList $ a
52+
toArgumentList (Just (Array a)) = Payload $ filter (/= Aeson.Null) . toList $ a
5653
toArgumentList _ = Payload []
5754

5855
parseJSON _ = return NoEvent

Web/SocketIO/Types/String.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Web.SocketIO.Types.String (
1919

2020

2121
--------------------------------------------------------------------------------
22+
import qualified Data.Aeson as Aeson
2223
import qualified Data.String as S
2324
import qualified Data.Text as T
2425
import qualified Data.Text.Encoding as TE
@@ -155,6 +156,9 @@ class Serializable a where
155156
, Show a) => a -> s
156157
serialize = S.fromString . show
157158

159+
instance Serializable Aeson.Value where
160+
serialize = fromLazyByteString . Aeson.encode
161+
158162
instance Serializable T.Text where
159163
serialize = fromText
160164

@@ -163,7 +167,7 @@ instance Serializable TL.Text where
163167

164168
instance Serializable ByteString where
165169
serialize = fromByteString
166-
170+
167171
instance Serializable BL.ByteString where
168172
serialize = fromLazyByteString
169173

0 commit comments

Comments
 (0)