-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathV2Example.re
46 lines (35 loc) · 1.37 KB
/
V2Example.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
let socket = V2.Client.createWithUrl("https://localhost:8080");
open V2.Socket;
let logConnectAttempt = attempt => {
Js.log("Attempt number: " ++ Belt.Int.toString(attempt));
};
// Connect Events
socket->onConnect(() => {Js.log("connected")});
socket->onConnectError(err => {Js.log("Error happened: " ++ err.message)});
// Reconnect Events
socket->onReconnect(logConnectAttempt);
socket->onReconnecting(logConnectAttempt);
socket->onReconnectFailed(() => {Js.log("reconnect failed")});
socket->onEvent("news", arg => {
// Different ways to handle `arg` here
// 1) Either use Js.Json.classify (low level JSON decoding layer)
// 2) Use libraries such as bs-json / bs-decode / ...
// 3) Use a unsafe raw / %identity function for unsafe coercion
/*switch(arg->Js.Json.classify) {*/
/*| Js.Json.String => */
/*};*/
let result =
Js.Json.stringifyAny(arg)
->Belt.Option.getWithDefault("Could not stringify arg");
Js.log("news event happened: " ++ result);
});
socket->onEvent2("news", (arg1, arg2) => {
Js.log3("News event happened", arg1, arg2)
});
socket->onEvent3("news", (arg1, arg2, arg3) => {
Js.log4("News event happened", arg1, arg2, arg3)
});
socket->onPing(() => Js.log("ping occurred"));
socket->onPong(latency => Js.log2("pong occurred. Latency: ", latency));
socket->onError(err => {Js.log("An error event occurred: " ++ err.message)});
socket->connect;