@@ -13,6 +13,7 @@ module Reflex.Dom.WebSocket.Foreign
1313import Prelude hiding (all, concat, concatMap, div, mapM, mapM_, sequence, span)
1414
1515import Control.Monad.Reader
16+import Data.Bifoldable
1617import Data.ByteString (ByteString)
1718import Data.Text (Text)
1819import qualified Data.Text as T (unpack)
@@ -21,8 +22,9 @@ import Foreign.JavaScript.Utils (bsFromMutableArrayBuffer, bsToArrayBuffer)
2122import GHCJS.DOM.CloseEvent
2223import GHCJS.DOM.EventM (on)
2324import GHCJS.DOM.MessageEvent
24−import GHCJS.DOM.Types hiding (Text, ByteString)
25−import qualified GHCJS.DOM.WebSocket as GD
25+import GHCJS.DOM.Types (JSM, JSVal, liftJSM, fromJSValUnchecked)
26+import GHCJS.DOM.WebSocket (WebSocket)
27+import qualified GHCJS.DOM.WebSocket as DOM
2628import GHCJS.Foreign (JSType(..), jsTypeOf)
2729import Language.Javascript.JSaddle.Helper (mutableArrayBufferFromJSVal)
2830import Language.Javascript.JSaddle.Types (ghcjsPure)
@@ -32,19 +34,22 @@ newtype JSWebSocket = JSWebSocket { unWebSocket :: WebSocket }
3234class IsWebSocketMessage a where
3335 webSocketSend :: JSWebSocket -> a -> JSM ()
3436
37+instance (IsWebSocketMessage a, IsWebSocketMessage b) => IsWebSocketMessage (Either a b) where
38+ webSocketSend jws = bitraverse_ (webSocketSend jws) (webSocketSend jws)
39+
3540-- Use binary websocket communication for ByteString
3641-- Note: Binary websockets may not work correctly in IE 11 and below
3742instance IsWebSocketMessage ByteString where
3843 webSocketSend (JSWebSocket ws) bs = do
3944 ab <- bsToArrayBuffer bs
40− GD.send ws ab
45+ DOM.send ws ab
4146
4247-- Use plaintext websocket communication for Text, and String
4348instance IsWebSocketMessage Text where
44− webSocketSend (JSWebSocket ws) = GD.sendString ws . T.unpack
49+ webSocketSend (JSWebSocket ws) = DOM.sendString ws . T.unpack
4550
4651closeWebSocket :: 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)
4853
4954newWebSocket
5055 :: a
@@ -55,17 +60,17 @@ newWebSocket
5560 -> ((Bool, Word, Text) -> JSM ()) -- onclose
5661 -> JSM JSWebSocket
5762newWebSocket _ url onMessage onOpen onError onClose = do
58− ws <- GD.newWebSocket url ([] :: [Text])
59− GD.setBinaryType ws "arraybuffer"
60− _ <- on ws GD.open $ liftJSM onOpen
61− _ <- on ws GD.error $ liftJSM onError
62− _ <- on ws GD.closeEvent $ do
63+ ws <- DOM.newWebSocket url ([] :: [Text])
64+ DOM.setBinaryType ws "arraybuffer"
65+ _ <- on ws DOM.open $ liftJSM onOpen
66+ _ <- on ws DOM.error $ liftJSM onError
67+ _ <- on ws DOM.closeEvent $ do
6368 e <- ask
6469 wasClean <- getWasClean e
6570 code <- getCode e
6671 reason <- getReason e
6772 liftJSM $ onClose (wasClean, code, reason)
68− _ <- on ws GD.message $ do
73+ _ <- on ws DOM.message $ do
6974 e <- ask
7075 d <- getData e
7176 liftJSM $ ghcjsPure (jsTypeOf d) >>= \case