twentefp-websockets-0.1.0.0: A fork of the popular websockets package. It is used for the practical assignments of the University of Twente. A sensible and clean way to write WebSocket-capable servers in Haskell.

Safe HaskellNone
LanguageHaskell98

Network.WebSockets.Connection

Synopsis

Documentation

makeSocket :: String -> Int -> IO Socket Source

Create a standardized socket. Should only be used for a quick and dirty solution! Should be preceded by the call Network.Socket.withSocketsDo

closeSocket :: Socket -> IO () Source

Closes a socket. This function serves as a quick utility to close a socket and as a reminder that you need to close sockets made by makeSocket.

data PendingConnection Source

A new client connected to the server. We haven't accepted the connection yet, though.

Constructors

PendingConnection 

Fields

pendingOptions :: ConnectionOptions

Options, passed as-is to the Connection

pendingRequest :: RequestHead

Useful for e.g. inspecting the request path.

pendingOnAccept :: Connection -> IO ()

One-shot callback fired when a connection is accepted, i.e., *after* the accepting response is sent to the client.

pendingIn :: InputStream ByteString

Input stream

pendingOut :: OutputStream Builder

Output stream

makePendingConnection :: Socket -> IO PendingConnection Source

Use data from the socket to create a Pending Connection. This is a blocking function. It tries to first accept a connection before creating a pending connection. Then you are able to choose if you want to accept the connection or not.

data AcceptRequest Source

Constructors

AcceptRequest 

Fields

acceptSubprotocol :: Maybe ByteString

The subprotocol to speak with the client. If pendingSubprotcols is non-empty, acceptSubprotocol must be one of the subprotocols from the list.

data Connection Source

Constructors

Connection 

Fields

connectionOptions :: ConnectionOptions
 
connectionType :: ConnectionType
 
connectionProtocol :: Protocol
 
connectionIn :: InputStream Message
 
connectionOut :: OutputStream Message
 
connectionSentClose :: IORef Bool

According to the RFC, both the client and the server MUST send a close control message to each other. Either party can initiate the first close message but then the other party must respond. Finally, the server is in charge of closing the TCP connection. This IORef tracks if we have sent a close message and are waiting for the peer to respond.

receiveDataMessage :: Connection -> IO DataMessage Source

Receive an application message. Automatically respond to control messages.

When the peer sends a close control message, an exception of type CloseRequest is thrown. The peer can send a close control message either to initiate a close or in response to a close message we have sent to the peer. In either case the CloseRequest exception will be thrown. The RFC specifies that the server is responsible for closing the TCP connection, which should happen after receiving the CloseRequest exception from this function.

This will throw ConnectionClosed if the TCP connection dies unexpectedly.

receiveData :: WebSocketsData a => Connection -> IO a Source

Receive a message, converting it to whatever format is needed.

sendTextData :: WebSocketsData a => Connection -> a -> IO () Source

Send a message as text

sendBinaryData :: WebSocketsData a => Connection -> a -> IO () Source

Send a message as binary data

sendClose :: WebSocketsData a => Connection -> a -> IO () Source

Send a friendly close message. Note that after sending this message, you should still continue calling receiveDataMessage to process any in-flight messages. The peer will eventually respond with a close control message of its own which will cause receiveDataMessage to throw the CloseRequest exception. This exception is when you can finally consider the connection closed.

sendCloseCode :: WebSocketsData a => Connection -> Word16 -> a -> IO () Source

Send a friendly close message and close code. Similar to sendClose, you should continue calling receiveDataMessage until you receive a CloseRequest exception. See http://tools.ietf.org/html/rfc6455#section-7.4 for a list of close codes.

sendPing :: WebSocketsData a => Connection -> a -> IO () Source

Send a ping