|
| 1 | +module DOM.Websocket.WebSocket |
| 2 | + ( create |
| 3 | + , url |
| 4 | + , readyState |
| 5 | + , bufferedAmount |
| 6 | + , extensions |
| 7 | + , protocol |
| 8 | + , close |
| 9 | + , getBinaryType |
| 10 | + , setBinaryType |
| 11 | + , sendString |
| 12 | + , sendBlob |
| 13 | + , sendArrayBuffer |
| 14 | + , sendArrayBufferView |
| 15 | + , module DOM.Websocket.BinaryType |
| 16 | + , module DOM.Websocket.Event.Types |
| 17 | + , module DOM.Websocket.ReadyState |
| 18 | + , module DOM.Websocket.Types |
| 19 | + ) where |
| 20 | + |
| 21 | +import Prelude |
| 22 | + |
| 23 | +import Control.Monad.Eff (Eff) |
| 24 | + |
| 25 | +import Data.ArrayBuffer.Types (ArrayBuffer, ArrayView) |
| 26 | +import Data.Foreign (Foreign, toForeign) |
| 27 | +import Data.Maybe (fromJust) |
| 28 | + |
| 29 | +import DOM (DOM) |
| 30 | +import DOM.File.Types (Blob) |
| 31 | +import DOM.Websocket.BinaryType (BinaryType(..), fromEnumBinaryType, printBinaryType, toEnumBinaryType) |
| 32 | +import DOM.Websocket.Event.Types (CloseEvent, MessageEvent, readCloseEvent, readMessageEvent) |
| 33 | +import DOM.Websocket.ReadyState (ReadyState(..), fromEnumReadyState, toEnumReadyState) |
| 34 | +import DOM.Websocket.Types (Protocol(..), URL(..), WebSocket, readWebSocket, socketToEventTarget) |
| 35 | + |
| 36 | +import Partial.Unsafe (unsafePartial) |
| 37 | + |
| 38 | +foreign import create :: forall eff. URL -> Array Protocol -> Eff (dom :: DOM | eff) WebSocket |
| 39 | + |
| 40 | +foreign import url :: forall eff. WebSocket -> Eff (dom :: DOM | eff) String |
| 41 | + |
| 42 | +foreign import readyStateImpl :: forall eff. WebSocket -> Eff (dom :: DOM | eff) Int |
| 43 | + |
| 44 | +readyState :: forall eff. WebSocket -> Eff (dom :: DOM | eff) ReadyState |
| 45 | +readyState ws = do |
| 46 | + rs <- readyStateImpl ws |
| 47 | + pure $ unsafePartial $ fromJust $ toEnumReadyState rs |
| 48 | + |
| 49 | +foreign import bufferedAmount :: forall eff. WebSocket -> Eff (dom :: DOM | eff) Number |
| 50 | + |
| 51 | +foreign import extensions :: forall eff. WebSocket -> Eff (dom :: DOM | eff) String |
| 52 | +foreign import protocol :: forall eff. WebSocket -> Eff (dom :: DOM | eff) String |
| 53 | + |
| 54 | +foreign import close :: forall eff. WebSocket -> Eff (dom :: DOM | eff) Unit |
| 55 | + |
| 56 | +foreign import getBinaryTypeImpl :: forall eff. WebSocket -> Eff (dom :: DOM | eff) String |
| 57 | +foreign import setBinaryTypeImpl :: forall eff. WebSocket -> String -> Eff (dom :: DOM | eff) Unit |
| 58 | + |
| 59 | +getBinaryType :: forall eff. WebSocket -> Eff (dom :: DOM | eff) BinaryType |
| 60 | +getBinaryType ws = unsafePartial do |
| 61 | + getBinaryTypeImpl ws <#> case _ of |
| 62 | + "blob" -> Blob |
| 63 | + "arraybuffer" -> ArrayBuffer |
| 64 | + |
| 65 | +setBinaryType :: forall eff. WebSocket -> BinaryType -> Eff (dom :: DOM | eff) Unit |
| 66 | +setBinaryType ws = setBinaryTypeImpl ws <<< printBinaryType |
| 67 | + |
| 68 | +foreign import sendImpl :: forall eff. WebSocket -> Foreign -> Eff (dom :: DOM | eff) Unit |
| 69 | + |
| 70 | +sendString :: forall eff. WebSocket -> String -> Eff (dom :: DOM | eff) Unit |
| 71 | +sendString ws = sendImpl ws <<< toForeign |
| 72 | + |
| 73 | +sendBlob :: forall eff. WebSocket -> Blob -> Eff (dom :: DOM | eff) Unit |
| 74 | +sendBlob ws = sendImpl ws <<< toForeign |
| 75 | + |
| 76 | +sendArrayBuffer :: forall eff. WebSocket -> ArrayBuffer -> Eff (dom :: DOM | eff) Unit |
| 77 | +sendArrayBuffer ws = sendImpl ws <<< toForeign |
| 78 | + |
| 79 | +sendArrayBufferView :: forall t eff. WebSocket -> ArrayView t -> Eff (dom :: DOM | eff) Unit |
| 80 | +sendArrayBufferView ws = sendImpl ws <<< toForeign |
0 commit comments