websockets-0.3.1.0: A sensible and clean way to write WebSocket-capable servers in Haskell.

Network.WebSockets

Contents

Description

How do you use this library? Here's how:

  • Get an enumerator/iteratee pair from your favorite web server, or use runServer to set up a simple standalone server.
  • Read the Request using receiveRequest. Inspect its path and the perform the initial handshake. This yields a Response which you can send back using sendResponse. The WebSocket is now ready.

There are (informally) three ways in which you can use the library:

  • The most simple case: You don't care about the internal representation of the messages. In this case, use the WebSocketsData typeclass: receiveData, sendTextData and sendBinaryData will be useful.
  • You have some protocol, and it is well-specified in which cases the client should send text messages, and in which cases the client should send binary messages. In this case, you can use the receiveDataMessage and sendDataMessage methods.
  • You need to write a more low-level server in which you have control over control frames (e.g. ping/pong). In this case, you can use the receiveMessage and sendMessage methods.

In some cases, you want to escape from the WebSockets monad and send data to the websocket from different threads. To this end, the getSender method is provided.

For a full example, see:

http://github.com/jaspervdj/websockets/tree/master/example

Synopsis

WebSocket type

data WebSocketsOptions Source

Options for the WebSocket program

Constructors

WebSocketsOptions 

Fields

onPong :: IO ()
 
pingInterval :: Maybe Int
 

data WebSockets a Source

The monad in which you can write WebSocket-capable applications

runWebSockets :: WebSockets a -> Iteratee ByteString IO () -> Iteratee ByteString IO aSource

Run a WebSockets application on an 'Enumerator'/'Iteratee' pair.

runWebSocketsWith :: WebSocketsOptions -> WebSockets a -> Iteratee ByteString IO () -> Iteratee ByteString IO aSource

Version of runWebSockets which allows you to specify custom options

A simple standalone server

runServerSource

Arguments

:: String

Address to bind to

-> Int

Port to listen on

-> WebSockets ()

Application to serve

-> IO ()

Never returns

Provides a simple server. This function blocks forever. Note that this is merely provided for quick-and-dirty standalone applications, for real applications, you should use a real server.

runWithSocket :: Socket -> WebSockets a -> IO aSource

This function wraps runWebSockets in order to provide a simple API for stand-alone servers.

Types

type Headers = [(CI ByteString, ByteString)]Source

Request headers

data Request Source

Simple request type

Constructors

Request 

Instances

data Response Source

Response to a Request

Instances

data Frame Source

A frame

Constructors

Frame 

Instances

data Message Source

The kind of message a server application typically deals with

Instances

data ControlMessage Source

Different control messages

Instances

data DataMessage Source

For an end-user of this library, dealing with Frames would be a bit low-level. This is why define another type on top of it, which represents data for the application layer.

Instances

class WebSocketsData a whereSource

In order to have an even more high-level API, we define a typeclass for values the user can receive from and send to the socket. A few warnings apply:

  • Natively, everything is represented as a ByteString, so this is the fastest instance
  • You should only use the Text or the Text instance when you are sure that the data is UTF-8 encoded (which is the case for Text messages).
  • Messages can be very large. If this is the case, it might be inefficient to use the strict ByteString and Text instances.

Initial handshake

data HandshakeError Source

Error in case of failed handshake.

Constructors

HandshakeError String 

receiveRequest :: WebSockets (Maybe Request)Source

Read a Request from the socket. Blocks until one is received and returns Nothing if the socket has been closed.

sendResponse :: Response -> WebSockets ()Source

Send a Response to the socket immediately.

handshake :: Request -> Either HandshakeError ResponseSource

Provides the logic for the initial handshake defined in the WebSocket protocol. This function will provide you with a Response which accepts and upgrades the received Request. Once this Response is sent, you can start sending and receiving actual application data.

In the case of a malformed request, a HandshakeError is returned.

Sending and receiving

receiveFrame :: WebSockets (Maybe Frame)Source

Read a Frame from the socket. Blocks until a frame is received and returns Nothing if the socket has been closed.

Note that a typical library user will want to use something like receiveByteStringData instead.

sendFrame :: Frame -> WebSockets ()Source

A low-level function to send an arbitrary frame over the wire.

sendMessage :: Message -> WebSockets ()Source

Send a message

receiveDataMessage :: WebSockets (Maybe DataMessage)Source

Receive an application message. Automatically respond to control messages.

sendDataMessage :: DataMessage -> WebSockets ()Source

Send an application-level message.

receiveData :: WebSocketsData a => WebSockets (Maybe a)Source

Receive a message, treating it as data transparently

sendTextData :: WebSocketsData a => a -> WebSockets ()Source

Send a text message

sendBinaryData :: WebSocketsData a => a -> WebSockets ()Source

Send some binary data

Advanced sending

type Encoder a = Mask -> a -> BuilderSource

The inverse of a parser

type Sender a = Encoder a -> a -> IO ()Source

For asynchronous sending

send :: Encoder a -> a -> WebSockets ()Source

Low-level sending with an arbitrary Encoder

getSender :: WebSockets (Sender a)Source

In case the user of the library wants to do asynchronous sending to the socket, he can extract a Sender and pass this value around, for example, to other threads.

response :: Encoder ResponseSource

Encode an HTTP upgrade response

frame :: Encoder FrameSource

Encode a frame

message :: Encoder MessageSource

Encode a message

controlMessage :: Encoder ControlMessageSource

Encode a control message

dataMessage :: Encoder DataMessageSource

Encode an application message