mirror of
https://github.com/Tencent/Hippy.git
synced 2026-10-20 15:51:35 +08:00
26e6434b75
- hippy-vue: ElementNode add repaintWithChildren API - hippy-vue: support remove outdated style of HMR chunk - hippy-vue-loader: support style hmr - hippy-vue-css-loader: support remove outdated style of HMR chunk - debug-server, vue-demo, react-demo, hippy-react: support hmr - debug-server: add hippy-dev command
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
import WebSocketClient from './clients/WebSocketClient';
|
|
import { log } from './utils/log';
|
|
|
|
let retries = 0;
|
|
let maxRetries = 10;
|
|
let client = null;
|
|
|
|
const socket = function initSocket(url, handlers, reconnect) {
|
|
client = new WebSocketClient(url);
|
|
|
|
client.onOpen(() => {
|
|
retries = 0;
|
|
maxRetries = reconnect;
|
|
});
|
|
|
|
client.onClose(() => {
|
|
if (retries === 0) {
|
|
handlers.close();
|
|
}
|
|
|
|
// Try to reconnect.
|
|
client = null;
|
|
|
|
// After 10 retries stop trying, to prevent logspam.
|
|
if (retries < maxRetries) {
|
|
// Exponentially increase timeout to reconnect.
|
|
// Respectfully copied from the package `got`.
|
|
// eslint-disable-next-line no-mixed-operators, no-restricted-properties
|
|
const retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;
|
|
|
|
retries += 1;
|
|
|
|
log.info('Trying to reconnect...');
|
|
|
|
setTimeout(() => {
|
|
socket(url, handlers, reconnect);
|
|
}, retryInMs);
|
|
}
|
|
});
|
|
|
|
client.onMessage((data) => {
|
|
const message = JSON.parse(data);
|
|
log.info(message);
|
|
|
|
if (handlers[message.type]) {
|
|
handlers[message.type](message.data, message.params);
|
|
}
|
|
});
|
|
};
|
|
|
|
export default socket;
|