socket-io-1.3.6

Safe HaskellNone
LanguageHaskell2010

Network.SocketIO

Contents

Synopsis

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)) => 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 => 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 => 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

To One Client

emit :: (ToJSON a, MonadReader Socket m, MonadIO m) => Text -> a -> m () Source #

Emit an event and argument data to a Socket. If called from within on, this will be the client that emitted the original event.

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 Sockets.

broadcastJSON :: (MonadReader Socket m, MonadIO m) => Text -> Array -> m () Source #

Broadcast an event with an array of JSON arguments to all other Sockets.

Sockets

data Socket Source #

A Socket.IO socket (not to be confused with an Engine.IO Socket).

Instances

socketId :: Socket -> SocketId Source #

Retrieve the Engine.IO SocketId for a 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

Packets

data Packet Source #

Constructors

Packet !PacketType !(Maybe Int) !Text !(Maybe Int) !(Maybe Value) 

Instances