Safe Haskell | None |
---|
- initialize :: MonadIO m => ServerAPI m -> StateT RoutingTable m a -> IO (m ())
- data RoutingTable
- on :: (MonadState RoutingTable m, FromJSON arg, Applicative m) => Text -> (arg -> EventHandler a) -> m ()
- on_ :: (MonadState RoutingTable m, Applicative m) => Text -> EventHandler a -> m ()
- onJSON :: (MonadState RoutingTable m, Applicative m) => Text -> (Array -> EventHandler a) -> m ()
- appendDisconnectHandler :: MonadState RoutingTable m => (SocketId -> IO ()) -> m ()
- type EventHandler a = ReaderT Socket IO a
- emit :: (ToJSON a, MonadReader Socket m, MonadIO m) => Text -> a -> m ()
- emitJSON :: (MonadReader Socket m, MonadIO m) => Text -> Array -> m ()
- emitTo :: (ToJSON a, MonadIO m) => Socket -> Text -> a -> m ()
- emitJSONTo :: MonadIO m => Socket -> Text -> Array -> m ()
- broadcast :: (ToJSON a, MonadReader Socket m, MonadIO m) => Text -> a -> m ()
- broadcastJSON :: (MonadReader Socket m, MonadIO m) => Text -> Array -> m ()
- data Socket
- socketId :: Socket -> SocketId
- data PacketType
- = Connect
- | Disconnect
- | Event
- | Ack
- | Error
- | BinaryEvent
- | BinaryAck
- parsePacketType :: Parser PacketType
- encodePacketType :: PacketType -> Builder
- data Packet = Packet !PacketType !(Maybe Int) !Text !(Maybe Int) !(Maybe Value)
- parsePacket :: Parser Packet
- encodePacket :: Packet -> Builder
Documentation
This library provides an implementation of Socket.io protocol (version 1). It builds on top of Engine.IO, allowing Socket.io to work with both long polling XHR requests, and seamlessly upgrading them to HTML 5 web sockets.
initialize :: MonadIO m => ServerAPI m -> StateT RoutingTable m a -> IO (m ())Source
This computation initializes a Socket.IO server and returns a computation that
you should call whenever a request comes in to the socket.io
path. For
example, in a Snap application, you might do:
handler <- initialize snapAPI mkRoutes quickHttpServe $ route [("/socket.io", handler)]
The second argument to this function is an action to build up the routing table,
which determines what happens when clients emit events. It is also an action
that is called every time a client connects, so you can mutate state by taking
advantage of the MonadIO
instance. You can build a routing table by using the
convenience on
family of functions.
Receiving events
data RoutingTable Source
A per-connection routing table. This table determines what actions to invoke when events are received.
on :: (MonadState RoutingTable m, FromJSON arg, Applicative m) => Text -> (arg -> EventHandler a) -> m ()Source
When an event with a given name is received, and its arguments can be
decoded by a FromJSON
instance, run the associated function
after decoding the event arguments.
on_ :: (MonadState RoutingTable m, Applicative m) => Text -> EventHandler a -> m ()Source
When an event is received with a given name and no arguments, run the
associated EventHandler
.
onJSON :: (MonadState RoutingTable m, Applicative m) => Text -> (Array -> EventHandler a) -> m ()Source
When an event with a given name is received, call the associated function with the array of JSON arguments.
appendDisconnectHandler :: MonadState RoutingTable m => (SocketId -> IO ()) -> m ()Source
Run the given IO action when a client disconnects, along with any other previously register disconnect handlers.
Emitting Events
type EventHandler a = ReaderT Socket IO aSource
To One Client
emitJSON :: (MonadReader Socket m, MonadIO m) => Text -> Array -> m ()Source
Emit an event with a specific array of JSON
arguments.
emitTo :: (ToJSON a, MonadIO m) => Socket -> Text -> a -> m ()Source
Emit an event to specific Socket
.
emitJSONTo :: MonadIO m => Socket -> Text -> Array -> m ()Source
Emit an event with a specific array of JSON
arguments to a specific
Socket
.
To Many Clients
broadcast :: (ToJSON a, MonadReader Socket m, MonadIO m) => Text -> a -> m ()Source
Broadcast an event to all other Socket
s.
broadcastJSON :: (MonadReader Socket m, MonadIO m) => Text -> Array -> m ()Source
Broadcast an event with an array of JSON arguments to all other
Socket
s.
Sockets
A Socket.IO socket (not to be confused with an Engine.IO Socket
).
Protocol Types
Packet Types
data PacketType Source
Packets
encodePacket :: Packet -> BuilderSource