Safe Haskell | None |
---|---|
Language | Haskell2010 |
Network.SocketIO
Contents
- initialize :: MonadIO m => ServerAPI m -> StateT RoutingTable (ReaderT Socket m) a -> IO (m ())
- data RoutingTable
- on :: (MonadState RoutingTable m, OnArgs f (EventHandler a), Applicative m) => Text -> f -> 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 => EventHandler () -> 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
- engineIOSocket :: Socket -> Socket
- 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 (ReaderT Socket 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, OnArgs f (EventHandler a), Applicative m) => Text -> f -> m () Source
When an event with a given name is received, and its argument can be
decoded by a FromJSON
instance, run the associated function
after decoding the event argument. Expects exactly one event argument.
on_ :: (MonadState RoutingTable m, Applicative m) => Text -> EventHandler a -> m () Source
Deprecated: Use Network.SocketIO.on instead
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 => EventHandler () -> 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 a Source
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
).
engineIOSocket :: Socket -> Socket Source
Retrieve the Engine.IO Socket
that underlies this Socket.IO socket.
This is a fairly low-level operation - you should take care when reading or
writing directly to this socket, as it is possible to break invariants that
Socket.io is expecting.
Protocol Types
Packet Types
data PacketType Source
Constructors
Connect | |
Disconnect | |
Event | |
Ack | |
Error | |
BinaryEvent | |
BinaryAck |
Instances
Packets
encodePacket :: Packet -> Builder Source