|
| 1 | +const BinarySocket = require('./socket_base'); |
| 2 | + |
| 3 | +/* |
| 4 | + * Monitors the network status and initialises the WebSocket connection |
| 5 | + * 1. online : check the WS status (init/send: blink after timeout, open/message: online) |
| 6 | + * 2. offline: it is offline |
| 7 | + */ |
| 8 | +const NetworkMonitorBase = (() => { |
| 9 | + const status_config = { |
| 10 | + online : { class: 'online', tooltip: 'Online' }, |
| 11 | + offline : { class: 'offline', tooltip: 'Offline' }, |
| 12 | + blinking: { class: 'blinker', tooltip: 'Connecting to server' }, |
| 13 | + }; |
| 14 | + const pendings = {}; |
| 15 | + const pending_keys = { |
| 16 | + ws_init : 'ws_init', |
| 17 | + ws_request: 'ws_request', |
| 18 | + }; |
| 19 | + const pending_timeouts = { |
| 20 | + [pending_keys.ws_init] : 5000, |
| 21 | + [pending_keys.ws_request]: 10000, |
| 22 | + }; |
| 23 | + |
| 24 | + let ws_config, |
| 25 | + network_status, |
| 26 | + updateUI; |
| 27 | + |
| 28 | + const init = (socket_general_functions, fncUpdateUI) => { |
| 29 | + updateUI = fncUpdateUI; |
| 30 | + ws_config = Object.assign({ wsEvent, isOnline }, socket_general_functions); |
| 31 | + |
| 32 | + if ('onLine' in navigator) { |
| 33 | + window.addEventListener('online', setStatus); |
| 34 | + window.addEventListener('offline', setStatus); |
| 35 | + } else { // if not supported, default to online and fallback to WS checks |
| 36 | + navigator.onLine = true; |
| 37 | + } |
| 38 | + |
| 39 | + if (isOnline()) { |
| 40 | + BinarySocket.init(ws_config); |
| 41 | + } |
| 42 | + |
| 43 | + setStatus(isOnline() ? 'online' : 'offline'); |
| 44 | + }; |
| 45 | + |
| 46 | + const isOnline = () => navigator.onLine; |
| 47 | + |
| 48 | + const wsReconnect = () => { |
| 49 | + if (isOnline() && BinarySocket.hasReadyState(2, 3)) { // CLOSING or CLOSED |
| 50 | + BinarySocket.init(ws_config); |
| 51 | + } else { |
| 52 | + BinarySocket.send({ ping: 1 }); // trigger a request to get stable status sooner |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const setStatus = (status) => { |
| 57 | + if (!isOnline()) { |
| 58 | + network_status = 'offline'; |
| 59 | + } else if (pending_keys[status] || network_status === 'offline') { |
| 60 | + network_status = 'blinking'; |
| 61 | + wsReconnect(); |
| 62 | + } else { |
| 63 | + network_status = 'online'; |
| 64 | + } |
| 65 | + |
| 66 | + if (typeof updateUI === 'function') { |
| 67 | + updateUI(status_config[network_status], isOnline()); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const ws_events_map = { |
| 72 | + init : () => setPending(pending_keys.ws_init), |
| 73 | + open : () => clearPendings(pending_keys.ws_init), |
| 74 | + send : () => setPending(pending_keys.ws_request), |
| 75 | + message: () => clearPendings(), |
| 76 | + close : () => setPending(pending_keys.ws_init), |
| 77 | + }; |
| 78 | + |
| 79 | + const wsEvent = (event) => { |
| 80 | + if (typeof ws_events_map[event] === 'function') { |
| 81 | + ws_events_map[event](); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const setPending = (key) => { |
| 86 | + if (!pendings[key]) { |
| 87 | + pendings[key] = setTimeout(() => { |
| 88 | + pendings[key] = undefined; |
| 89 | + setStatus(key); |
| 90 | + }, pending_timeouts[key]); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const clearPendings = (key) => { |
| 95 | + const clear = (k) => { |
| 96 | + clearTimeout(pendings[k]); |
| 97 | + pendings[k] = undefined; |
| 98 | + if (k === pending_keys.ws_request) { |
| 99 | + setStatus('online'); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + if (key) { |
| 104 | + clear(key); |
| 105 | + } else { |
| 106 | + Object.keys(pendings).forEach(clear); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + return { |
| 111 | + init, |
| 112 | + wsEvent, |
| 113 | + }; |
| 114 | +})(); |
| 115 | + |
| 116 | +module.exports = NetworkMonitorBase; |
0 commit comments