Safe Haskell | None |
---|---|
Language | Haskell2010 |
Taken from wai-websockets package and customized to get IP address on websocket connection http://hackage.haskell.org/package/wai-websockets-3.0.1.2/docs/Network-Wai-Handler-WebSockets.html
Synopsis
- isWebSocketsReq :: Request -> Bool
- websocketsOr :: ConnectionOptions -> (IpAddress -> PendingConnection -> IO ()) -> Application -> Application
- websocketsApp :: ConnectionOptions -> (IpAddress -> PendingConnection -> IO ()) -> Request -> Maybe Response
- getRequestHead :: Request -> (RequestHead, IpAddress)
- runWebSockets :: ConnectionOptions -> RequestHead -> IpAddress -> (IpAddress -> PendingConnection -> IO a) -> IO ByteString -> (ByteString -> IO ()) -> IO a
Documentation
isWebSocketsReq :: Request -> Bool Source #
Returns whether or not the given Request
is a WebSocket request.
websocketsOr :: ConnectionOptions -> (IpAddress -> PendingConnection -> IO ()) -> Application -> Application Source #
Upgrade a websockets
ServerApp
to a wai
Application
. Uses
the given backup Application
to handle Request
s that are not
WebSocket requests.
websocketsOr opts ws_app backup_app = \req respond -> casewebsocketsApp
opts ws_app req ofNothing
-> backup_app req send_responseJust
res -> respond res
For example, below is an Application
that sends "Hello, client!"
to
each connected client.
app ::Application
app =websocketsOr
defaultConnectionOptions
wsApp backupApp where wsApp ::ServerApp
wsApp pending_conn = do conn <-acceptRequest
pending_connsendTextData
conn ("Hello, client!" ::Text
) backupApp ::Application
backupApp _ respond = respond $responseLBS
status400
[] "Not a WebSocket request"
websocketsApp :: ConnectionOptions -> (IpAddress -> PendingConnection -> IO ()) -> Request -> Maybe Response Source #
Handle a single wai
Request
with the given websockets
ServerApp
. Returns Nothing
if the Request
is not a WebSocket
request, Just
otherwise.
Usually, websocketsOr
is more convenient.
getRequestHead :: Request -> (RequestHead, IpAddress) Source #
runWebSockets :: ConnectionOptions -> RequestHead -> IpAddress -> (IpAddress -> PendingConnection -> IO a) -> IO ByteString -> (ByteString -> IO ()) -> IO a Source #
Internal function to run the WebSocket io-streams using the conduit library.