项目作者: dfordivam

项目描述 :
A simple to use interface for websocket using reflex-frp
高级语言: Haskell
项目地址: git://github.com/dfordivam/reflex-websocket-interface.git
创建时间: 2017-05-23T07:57:00Z
项目社区:https://github.com/dfordivam/reflex-websocket-interface

开源协议:GNU General Public License v3.0

下载


Consider using https://github.com/reflex-frp/reflex-gadt-api which implements websockets + XHR

Websocket Interface for Reflex applications

This repository consists of three packages to be used together, along with reflex as Dom builder, to do websocket communication.

Highlights

Uses type operators and Generic to create the request sum type, and avoid writing most of the server side boiler plate code.

type Request = Request1 :<|> Request2 :<|> Request3

  1. -- Well almost... see below for exact usage
  2. handler = handleRequest1 :<&> handleRequest2 :<&> handleRequest3
  1. Request <-> Response

    Type-checker make sure your client and server APIs are in sync.

  2. Completeness of server handler

    The Type-checker also make sure that all the requests are handled by the server side code.

  3. Client side monadic interface with routing of response event.

    In the client application the requests come from different parts of the DOM and their responses need to be routed back to the same place.
    This is all taken care by the library code and the user interface is as simple as calling an API

    getResponse :: Event t request -> m (Event t response)

Usage

See the code in example folder for more details.

  1. Shared code

    Create a shared code which has the websocket message type using the package reflex-websocket-interface-shared

    Define the types of all the requests and their corresponding responses.
    Also derive Generic instance of all the individual request and response types.
    This can be used to automatically derive the ToJSON and FromJSON instances also.

    The collection of all websocket requests which can happen over a single connection are grouped together using the type operator (:<|>).
    In the rest of document this type is called referred as the request-type.

    1. type Request = Request1 :<|> Request2 :<|> Request3
    2. data Request1 = Request1 Text
    3. deriving (Generic, Show)
    4. data Request2 = Request2 (Text, Text)
    5. deriving (Generic, Show)
    6. data Request3 = Request3 [Text]
    7. deriving (Generic, Show)
    8. data Response1 = Response1 Int
    9. deriving (Generic, Show)
    10. data Response2 = Response2 Text
    11. deriving (Generic, Show)
    12. data Response3 = Response3 (Text, Int)
    13. deriving (Generic, Show)

    Next specify the WebSocketMessage instances for each of individual requests contained in the request-type

    1. instance WebSocketMessage Request Request1 where
    2. type ResponseT Request Request1 = Response1
    3. instance WebSocketMessage Request Request2 where
    4. type ResponseT Request Request2 = Response2
    5. instance WebSocketMessage Request Request3 where
    6. type ResponseT Request Request3 = Response3
  2. Frontend

    In the reflex application use the reflex-websocket-interface package.

    Use the getWebSocketResponse API along with the other DomBuilder code to create the widget in the WithWebSocketT monad.

    1. -- req1 :: Event t Request1
    2. -- respEv1 :: Event t Response1
    3. respEv1 <- getWebSocketResponse req1

    and specify this widget in withWSConnection API along with the websocket url to run the widget using the websocket connection.

    1. (retVal,wsConn) <- withWSConnection
    2. url wsCloseEvent doRecconectBool widgetCode
  3. Server

    Use the reflex-websocket-interface-server package

    Specify all the handlers like this (m can be any monad, Use Identity monad if the handler code is pure)

    1. handleRequest1 :: (Monad m) => Request1 -> m Response1
    2. handleRequest2 :: (Monad m) => Request2 -> m Response2

    Create a main handler using all the individual handler using the type operator (:<&>) and the makeHandler API.
    You need to specify the request-type explicitly in the HandlerWrapper and makeHandler like this

    1. handler :: HandlerWrapper IO Request
    2. handler = HandlerWrapper $
    3. h handleRequest1
    4. :<&> h handleRequest2
    5. where
    6. h :: (WebSocketMessage Request a, Monad m)
    7. => (a -> m (ResponseT Request a))
    8. -> Handler m Request a
    9. h = makeHandler

    Use this handler in the handleRequest API, also specify the bytestring received from the websocket connection.
    This API will run the appropriate handler based on the request type and encode the response back in bytestring.

    1. -- resp :: Bytestring
    2. -- showF :: (Show a, Show b) => a -> b -> m ()
    3. resp <- handleRequest handler showF bsRecieved

Here I have used the aeson package for serialisation. To use some other serialisation package the library code will need slight modifications, but should work.