fay-websockets (empty) → 0.0.1.0
raw patch · 6 files changed
+158/−0 lines, 6 filesdep +fay-basesetup-changed
Dependencies added: fay-base
Files
- ChangeLog.md +5/−0
- LICENSE +30/−0
- README.md +27/−0
- Setup.hs +2/−0
- fay-websockets.cabal +33/−0
- src/WebSockets.hs +61/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for fay-websockets++## 0.0.1.0 -- 2018-11-28++* First version. Under development. Supported connection handling, sending/receiving of text data.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Andrey Prokopenko++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Andrey Prokopenko nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,27 @@+# fay-websockets++A FFI Wrapper for WebSockets use with Fay. It includes functions for WebSockets connection initialization, handling WebSockets events and sending data over WebSockets.++## Installation++With cabal:++```+cabal new-install fay-websockets+```++With stack:++```+stack build+```++Or just include `fay-websockets` in either your `.cabal` file or `package.yml`.++Then include it at the top of Fay file:++```+import WebSockets+```++2018 (c) Andrey Prokopenko
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ fay-websockets.cabal view
@@ -0,0 +1,33 @@+name: fay-websockets+version: 0.0.1.0+synopsis: Websockets FFI library for Fay+description: Websockets FFI library for Fay+homepage: https://github.com/swamp-agr/fay-websockets+bug-reports: https://github.com/swamp-agr/fay-websockets/issues+license: BSD3+license-file: LICENSE+author: Andrey Prokopenko+maintainer: persiantiger@yandex.ru+copyright: 2018 Andrey Prokopenko+category: Web, Fay+build-type: Simple+cabal-version: >=1.10+data-files: src/WebSockets.hs+extra-source-files: + ChangeLog.md+ LICENSE+ README.md+++source-repository head+ type: git+ location: https://github.com/swamp-agr/fay-websockets.git ++library+ hs-source-dirs: src+ exposed-modules: WebSockets+ ghc-options: -Wall+ build-depends:+ fay-base >= 0.19.4.0 && < 0.22++ default-language: Haskell2010
+ src/WebSockets.hs view
@@ -0,0 +1,61 @@+{-# LANGUAGE EmptyDataDecls #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE RebindableSyntax #-}+module WebSockets where++import Data.Text+import FFI++-- | WebSocket object to keep connection, handle events and sending data.+data WebSocket++-- | WebSocket event.+data WSEvent++-- | State of WebSocket+data WSState = Connecting | Open | Closing | Closed++-- | Get current url and replace 'http' with 'ws'.+getWsUrl :: Fay Text+getWsUrl = ffi "window['location']['href'].replace('http:', 'ws:').replace('https:', 'wss:')"++-- | Wrapper over '.onopen' event.+onOpen :: WSEvent -> Fay ()+onOpen = ffi "console.log('WebSocket is up and running')"++-- | Wrapper over '.onclose' event.+onClose :: WebSocket -> Fay f -> Fay ()+onClose = ffi "%1.onclose=%2"++-- | Wrapper over '.onError' event.+onError :: WebSocket -> Fay f -> Fay ()+onError = ffi "%1.onerror=%2"++-- | Get text data from event.+eventData :: WSEvent -> Fay Text+eventData = ffi "%1['data']"++-- | Wrapper over '.onMessage'+onMessage :: WSEvent -> Fay ()+onMessage = ffi "(function(e){console.log(e); console.log(e.data);})(%1)"++-- | Key function that by given URL initialize connection, starts to listen all events and ready to sending data.+websocket+ :: Text -- ^ WebSocket endpoint URL;+ -> (WSEvent -> Fay ()) -- ^ when connection opened ;+ -> (WSEvent -> Fay ()) -- ^ when messages received;+ -> (WSEvent -> Fay ()) -- ^ when errors occured;+ -> (WSEvent -> Fay ()) -- ^ when connection closed.+ -> Fay WebSocket+websocket = ffi "(function (){var conn = new WebSocket(%1);\+\ conn.onopen = %2, conn.onmessage = %3, conn.onerror = %4, conn.onclose = %5;\ +\return conn})()"++-- FIXME: add 'USVString', 'ArrayBuffer', 'Blob', 'ArrayBufferView'+-- | Send text data over WebSocket.+sendWS :: WebSocket -> Text -> Fay ()+sendWS = ffi "%1.send(%2)"++-- | Close current connection.+close :: WebSocket -> Fay ()+close = ffi "%1.close"