-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebsocketsExample.re
46 lines (44 loc) · 1.1 KB
/
WebsocketsExample.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
open WebapiExtra;
let readyStateToString =
fun
| Dom.WebSocket.Connecting => "Connecting"
| Open => "Open"
| Closing => "Closing"
| Closed => "Closed"
| Unknown => "Unknown";
let run = () => {
open Dom.WebSocket;
let ws = create("ws://localhost:7777");
Js.log("WS created... " ++ (ws->readyState |> readyStateToString));
ws
->setOnMessage(e => {
Js.log(e);
Js.log("WS message received: " ++ e->Dom.MessageEvent.data);
});
ws
->setOnError(e => {
Js.log(e);
Js.log("WS error..." ++ (ws->readyState |> readyStateToString));
});
ws
->setOnOpen(_ =>
Js.log("WS opened... " ++ (ws->readyState |> readyStateToString))
);
ws->setOnClose(e => Js.log(e));
open Webapi.Dom;
let maybeBtn = document |> Document.getElementById("send-ws-msg-btn");
switch (maybeBtn) {
| Some(btn) =>
btn
|> Element.addClickEventListener(_ =>
ws
->sendString(
switch (Js.Json.stringifyAny @@ {"type": "offer"}) {
| Some(str) => str
| None => ""
},
)
)
| None => ()
};
};