1 |
|
2 |
|
3 | import WebSocketClient from "./clients/WebSocketClient.js";
|
4 | import { log } from "./utils/log.js";
|
5 |
|
6 |
|
7 |
|
8 | var Client =
|
9 |
|
10 | typeof __webpack_dev_server_client__ !== "undefined" ? typeof __webpack_dev_server_client__.default !== "undefined" ? __webpack_dev_server_client__.default : __webpack_dev_server_client__ : WebSocketClient;
|
11 |
|
12 |
|
13 | var retries = 0;
|
14 | var maxRetries = 10;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export var client = null;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var socket = function initSocket(url, handlers, reconnect) {
|
27 | client = new Client(url);
|
28 | client.onOpen(function () {
|
29 | retries = 0;
|
30 | if (typeof reconnect !== "undefined") {
|
31 | maxRetries = reconnect;
|
32 | }
|
33 | });
|
34 | client.onClose(function () {
|
35 | if (retries === 0) {
|
36 | handlers.close();
|
37 | }
|
38 |
|
39 |
|
40 | client = null;
|
41 |
|
42 |
|
43 | if (retries < maxRetries) {
|
44 |
|
45 |
|
46 |
|
47 | var retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;
|
48 | retries += 1;
|
49 | log.info("Trying to reconnect...");
|
50 | setTimeout(function () {
|
51 | socket(url, handlers, reconnect);
|
52 | }, retryInMs);
|
53 | }
|
54 | });
|
55 | client.onMessage(
|
56 | |
57 |
|
58 |
|
59 | function (data) {
|
60 | var message = JSON.parse(data);
|
61 | if (handlers[message.type]) {
|
62 | handlers[message.type](message.data, message.params);
|
63 | }
|
64 | });
|
65 | };
|
66 | export default socket; |
\ | No newline at end of file |