servant-websockets-1.1.0: Small library providing WebSocket endpoints for servant.

Safe HaskellNone
LanguageHaskell2010

Servant.API.WebSocket

Synopsis

Documentation

data WebSocket Source #

Endpoint for defining a route to provide a web socket. The handler function gets an already negotiated websocket Connection to send and receive data.

Example:

type WebSocketApi = "stream" :> WebSocket

server :: Server WebSocketApi
server = streamData
 where
  streamData :: MonadIO m => Connection -> m ()
  streamData c = do
    liftIO $ forkPingThread c 10
    liftIO . forM_ [1..] $ \i -> do
       sendTextData c (pack $ show (i :: Int)) >> threadDelay 1000000

Instances

HasServer * WebSocket ctx Source # 

Associated Types

type ServerT WebSocket (ctx :: WebSocket) (m :: * -> *) :: * #

Methods

route :: Proxy WebSocket ctx -> Context context -> Delayed env (Server WebSocket ctx) -> Router env #

hoistServerWithContext :: Proxy WebSocket ctx -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT WebSocket ctx m -> ServerT WebSocket ctx n #

type ServerT * WebSocket m Source # 
type ServerT * WebSocket m = Connection -> m ()

data WebSocketPending Source #

Endpoint for defining a route to provide a web socket. The handler function gets a PendingConnection. It can either rejectRequest or acceptRequest. This function is provided for greater flexibility to reject connections.

Example:

type WebSocketApi = "stream" :> WebSocketPending

server :: Server WebSocketApi
server = streamData
 where
  streamData :: MonadIO m => PendingConnection -> m ()
  streamData pc = do
     c <- acceptRequest pc
     liftIO $ forkPingThread c 10
     liftIO . forM_ [1..] $ \i ->
       sendTextData c (pack $ show (i :: Int)) >> threadDelay 1000000

Instances

HasServer * WebSocketPending ctx Source # 

Associated Types

type ServerT WebSocketPending (ctx :: WebSocketPending) (m :: * -> *) :: * #

Methods

route :: Proxy WebSocketPending ctx -> Context context -> Delayed env (Server WebSocketPending ctx) -> Router env #

hoistServerWithContext :: Proxy WebSocketPending ctx -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT WebSocketPending ctx m -> ServerT WebSocketPending ctx n #

type ServerT * WebSocketPending m Source #