Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 903572f

Browse files
authored
Merge pull request #94 from purescript-contrib/ws
Add more of the WebSocket API
2 parents 3d9a6ad + 8fa91b2 commit 903572f

File tree

10 files changed

+312
-1
lines changed

10 files changed

+312
-1
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
},
55
"extends": "eslint:recommended",
66
"env": {
7-
"commonjs": true
7+
"commonjs": true,
8+
"browser": true
89
},
910
"rules": {
1011
"strict": [2, "global"],

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23+
"purescript-arraybuffer-types": "^1.0.0",
2324
"purescript-datetime": "^3.0.0",
2425
"purescript-enums": "^3.0.0",
2526
"purescript-exceptions": "^3.0.0",

src/DOM/Websocket/BinaryType.purs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module DOM.Websocket.BinaryType where
2+
3+
import Prelude
4+
import Data.Enum (Cardinality(..), class BoundedEnum, defaultPred, defaultSucc, class Enum)
5+
import Data.Maybe (Maybe(..))
6+
7+
data BinaryType
8+
= Blob
9+
| ArrayBuffer
10+
11+
derive instance eqBinaryType :: Eq BinaryType
12+
derive instance ordBinaryType :: Ord BinaryType
13+
14+
instance boundedBinaryType :: Bounded BinaryType where
15+
bottom = Blob
16+
top = ArrayBuffer
17+
18+
instance enumBinaryType :: Enum BinaryType where
19+
succ = defaultSucc toEnumBinaryType fromEnumBinaryType
20+
pred = defaultPred toEnumBinaryType fromEnumBinaryType
21+
22+
instance boundedEnumBinaryType :: BoundedEnum BinaryType where
23+
cardinality = Cardinality 3
24+
toEnum = toEnumBinaryType
25+
fromEnum = fromEnumBinaryType
26+
27+
instance showBinaryType :: Show BinaryType where
28+
show Blob = "Blob"
29+
show ArrayBuffer = "ArrayBuffer"
30+
31+
toEnumBinaryType :: Int -> Maybe BinaryType
32+
toEnumBinaryType =
33+
case _ of
34+
0 -> Just Blob
35+
1 -> Just ArrayBuffer
36+
_ -> Nothing
37+
38+
fromEnumBinaryType :: BinaryType -> Int
39+
fromEnumBinaryType =
40+
case _ of
41+
Blob -> 0
42+
ArrayBuffer -> 1
43+
44+
printBinaryType :: BinaryType -> String
45+
printBinaryType =
46+
case _ of
47+
Blob -> "blob"
48+
ArrayBuffer -> "arraybuffer"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module DOM.Websocket.Event.EventTypes where
2+
3+
import DOM.Event.Types (EventType(..))
4+
5+
onOpen :: EventType
6+
onOpen = EventType "open"
7+
8+
onMessage :: EventType
9+
onMessage = EventType "message"
10+
11+
onError :: EventType
12+
onError = EventType "error"
13+
14+
onClose :: EventType
15+
onClose = EventType "close"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
exports.data_ = function (e) {
4+
return e.data;
5+
};
6+
7+
exports.origin = function (e) {
8+
return e.origin;
9+
};
10+
11+
exports.lastEventId = function (e) {
12+
return e.lastEventId;
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module DOM.Websocket.Event.MessageEvent where
2+
3+
import Data.Foreign (Foreign)
4+
import DOM.Websocket.Event.Types (MessageEvent)
5+
6+
foreign import data_ :: MessageEvent -> Foreign
7+
foreign import origin :: MessageEvent -> String
8+
foreign import lastEventId :: MessageEvent -> String

src/DOM/Websocket/ReadyState.purs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module DOM.Websocket.ReadyState where
2+
3+
import Prelude
4+
import Data.Enum (Cardinality(..), class BoundedEnum, defaultPred, defaultSucc, class Enum)
5+
import Data.Maybe (Maybe(..))
6+
7+
data ReadyState
8+
= Connecting
9+
| Open
10+
| Closing
11+
| Closed
12+
13+
derive instance eqReadyState :: Eq ReadyState
14+
derive instance ordReadyState :: Ord ReadyState
15+
16+
instance boundedReadyState :: Bounded ReadyState where
17+
bottom = Connecting
18+
top = Closed
19+
20+
instance enumReadyState :: Enum ReadyState where
21+
succ = defaultSucc toEnumReadyState fromEnumReadyState
22+
pred = defaultPred toEnumReadyState fromEnumReadyState
23+
24+
instance boundedEnumReadyState :: BoundedEnum ReadyState where
25+
cardinality = Cardinality 3
26+
toEnum = toEnumReadyState
27+
fromEnum = fromEnumReadyState
28+
29+
instance showReadyState :: Show ReadyState where
30+
show Connecting = "Connecting"
31+
show Open = "Open"
32+
show Closing = "Closing"
33+
show Closed = "Closed"
34+
35+
toEnumReadyState :: Int -> Maybe ReadyState
36+
toEnumReadyState =
37+
case _ of
38+
0 -> Just Connecting
39+
1 -> Just Open
40+
2 -> Just Closing
41+
3 -> Just Closed
42+
_ -> Nothing
43+
44+
fromEnumReadyState :: ReadyState -> Int
45+
fromEnumReadyState =
46+
case _ of
47+
Connecting -> 0
48+
Open -> 1
49+
Closing -> 2
50+
Closed -> 3

src/DOM/Websocket/Types.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module DOM.Websocket.Types
2+
( module DOM.Websocket.Types
3+
, module DOM.HTML.History
4+
) where
5+
6+
import Prelude
7+
8+
import Data.Foreign (F, Foreign, unsafeReadTagged)
9+
import Data.Newtype (class Newtype)
10+
11+
import DOM.Event.Types (EventTarget)
12+
import DOM.HTML.History (URL(..))
13+
14+
import Unsafe.Coerce (unsafeCoerce)
15+
16+
foreign import data WebSocket :: Type
17+
18+
readWebSocket :: Foreign -> F WebSocket
19+
readWebSocket = unsafeReadTagged "WebSocket"
20+
21+
socketToEventTarget :: WebSocket -> EventTarget
22+
socketToEventTarget = unsafeCoerce
23+
24+
newtype Protocol = Protocol String
25+
26+
derive newtype instance eqProtocol :: Eq Protocol
27+
derive newtype instance ordProtocol :: Ord Protocol
28+
derive instance newtypeProtocol :: Newtype Protocol _

src/DOM/Websocket/WebSocket.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
exports.create = function (url) {
4+
return function (protocols) {
5+
return function () {
6+
return new WebSocket(url, protocols);
7+
};
8+
};
9+
};
10+
11+
exports.url = function (ws) {
12+
return function () {
13+
return ws.url;
14+
};
15+
};
16+
17+
exports.readyStateImpl = function (ws) {
18+
return function () {
19+
return ws.readyStateImpl;
20+
};
21+
};
22+
23+
exports.bufferedAmount = function (ws) {
24+
return function () {
25+
return ws.bufferedAmount;
26+
};
27+
};
28+
29+
exports.extensions = function (ws) {
30+
return function () {
31+
return ws.extensions;
32+
};
33+
};
34+
35+
exports.protocol = function (ws) {
36+
return function () {
37+
return ws.protocol;
38+
};
39+
};
40+
41+
exports.close = function (ws) {
42+
return function () {
43+
return ws.close();
44+
};
45+
};
46+
47+
exports.getBinaryTypeImpl = function (ws) {
48+
return function () {
49+
return ws.binaryType;
50+
};
51+
};
52+
53+
exports.setBinaryTypeImpl = function (ws) {
54+
return function (bt) {
55+
return function () {
56+
ws.binaryType = bt;
57+
};
58+
};
59+
};
60+
61+
exports.sendImpl = function (ws) {
62+
return function (value) {
63+
return function () {
64+
ws.send(value);
65+
};
66+
};
67+
};

src/DOM/Websocket/WebSocket.purs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)