@@ -13,6 +13,7 @@ module Reflex.Dom.WebSocket.Foreign
13import Prelude hiding (all, concat, concatMap, div, mapM, mapM_, sequence, span)
13import Prelude hiding (all, concat, concatMap, div, mapM, mapM_, sequence, span)
15import Control.Monad.Reader
15import Control.Monad.Reader
16+import Data.Bifoldable
16import Data.ByteString (ByteString)
17import Data.ByteString (ByteString)
17import Data.Text (Text)
18import Data.Text (Text)
18import qualified Data.Text as T (unpack)
19import qualified Data.Text as T (unpack)
@@ -21,8 +22,9 @@ import Foreign.JavaScript.Utils (bsFromMutableArrayBuffer, bsToArrayBuffer)
21import GHCJS.DOM.CloseEvent
22import GHCJS.DOM.CloseEvent
22import GHCJS.DOM.EventM (on)
23import GHCJS.DOM.EventM (on)
23import GHCJS.DOM.MessageEvent
24import GHCJS.DOM.MessageEvent
24−import GHCJS.DOM.Types hiding (Text, ByteString)
25+import GHCJS.DOM.Types (JSM, JSVal, liftJSM, fromJSValUnchecked)
26+import GHCJS.DOM.WebSocket (WebSocket)
25−import qualified GHCJS.DOM.WebSocket as GD
27+import qualified GHCJS.DOM.WebSocket as DOM
26import GHCJS.Foreign (JSType(..), jsTypeOf)
28import GHCJS.Foreign (JSType(..), jsTypeOf)
27import Language.Javascript.JSaddle.Helper (mutableArrayBufferFromJSVal)
29import Language.Javascript.JSaddle.Helper (mutableArrayBufferFromJSVal)
28import Language.Javascript.JSaddle.Types (ghcjsPure)
30import Language.Javascript.JSaddle.Types (ghcjsPure)
@@ -32,19 +34,22 @@ newtype JSWebSocket = JSWebSocket { unWebSocket :: WebSocket }
32class IsWebSocketMessage a where
34class IsWebSocketMessage a where
33 webSocketSend :: JSWebSocket -> a -> JSM ()
35 webSocketSend :: JSWebSocket -> a -> JSM ()
37+instance (IsWebSocketMessage a, IsWebSocketMessage b) => IsWebSocketMessage (Either a b) where
38+ webSocketSend jws = bitraverse_ (webSocketSend jws) (webSocketSend jws)
35-- Use binary websocket communication for ByteString
40-- Use binary websocket communication for ByteString
36-- Note: Binary websockets may not work correctly in IE 11 and below
41-- Note: Binary websockets may not work correctly in IE 11 and below
37instance IsWebSocketMessage ByteString where
42instance IsWebSocketMessage ByteString where
38 webSocketSend (JSWebSocket ws) bs = do
43 webSocketSend (JSWebSocket ws) bs = do
39 ab <- bsToArrayBuffer bs
44 ab <- bsToArrayBuffer bs
40− GD.send ws ab
45+ DOM.send ws ab
42-- Use plaintext websocket communication for Text, and String
47-- Use plaintext websocket communication for Text, and String
43instance IsWebSocketMessage Text where
48instance IsWebSocketMessage Text where
44− webSocketSend (JSWebSocket ws) = GD.sendString ws . T.unpack
49+ webSocketSend (JSWebSocket ws) = DOM.sendString ws . T.unpack
46closeWebSocket :: JSWebSocket -> Word -> Text -> JSM ()
51closeWebSocket :: JSWebSocket -> Word -> Text -> JSM ()
47−closeWebSocket (JSWebSocket ws) code reason = GD.close ws (Just code) (Just reason)
52+closeWebSocket (JSWebSocket ws) code reason = DOM.close ws (Just code) (Just reason)
49newWebSocket
54newWebSocket
@@ -55,17 +60,17 @@ newWebSocket
55 -> ((Bool, Word, Text) -> JSM ()) -- onclose
60 -> ((Bool, Word, Text) -> JSM ()) -- onclose
56 -> JSM JSWebSocket
61 -> JSM JSWebSocket
57newWebSocket _ url onMessage onOpen onError onClose = do
62newWebSocket _ url onMessage onOpen onError onClose = do
58− ws <- GD.newWebSocket url ([] :: [Text])
63+ ws <- DOM.newWebSocket url ([] :: [Text])
59− GD.setBinaryType ws "arraybuffer"
64+ DOM.setBinaryType ws "arraybuffer"
60− _ <- on ws GD.open $ liftJSM onOpen
65+ _ <- on ws DOM.open $ liftJSM onOpen
61− _ <- on ws GD.error $ liftJSM onError
66+ _ <- on ws DOM.error $ liftJSM onError
62− _ <- on ws GD.closeEvent $ do
67+ _ <- on ws DOM.closeEvent $ do
64 wasClean <- getWasClean e
69 wasClean <- getWasClean e
65 code <- getCode e
70 code <- getCode e
66 reason <- getReason e
71 reason <- getReason e
67 liftJSM $ onClose (wasClean, code, reason)
72 liftJSM $ onClose (wasClean, code, reason)
68− _ <- on ws GD.message $ do
73+ _ <- on ws DOM.message $ do
70 d <- getData e
75 d <- getData e
71 liftJSM $ ghcjsPure (jsTypeOf d) >>= \case
76 liftJSM $ ghcjsPure (jsTypeOf d) >>= \case