module Network.WebSockets.Types
( Message (..)
, ControlMessage (..)
, DataMessage (..)
, WebSocketsData (..)
, ConnectionError (..)
) where
import Control.Exception (Exception(..))
import Data.Typeable (Typeable)
import qualified Data.Attoparsec.Enumerator as AE
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
data ConnectionError
= ParseError AE.ParseError
| ConnectionClosed
deriving (Show, Typeable)
instance Exception ConnectionError
data Message p
= ControlMessage (ControlMessage p)
| DataMessage (DataMessage p)
deriving (Eq, Show)
data ControlMessage p
= Close BL.ByteString
| Ping BL.ByteString
| Pong BL.ByteString
deriving (Eq, Show)
data DataMessage p
= Text BL.ByteString
| Binary BL.ByteString
deriving (Eq, Show)
class WebSocketsData a where
fromLazyByteString :: BL.ByteString -> a
toLazyByteString :: a -> BL.ByteString
instance WebSocketsData BL.ByteString where
fromLazyByteString = id
toLazyByteString = id
instance WebSocketsData B.ByteString where
fromLazyByteString = B.concat . BL.toChunks
toLazyByteString = BL.fromChunks . return
instance WebSocketsData TL.Text where
fromLazyByteString = TL.decodeUtf8
toLazyByteString = TL.encodeUtf8
instance WebSocketsData T.Text where
fromLazyByteString = T.concat . TL.toChunks . fromLazyByteString
toLazyByteString = toLazyByteString . TL.fromChunks . return