function startWebsocket(url, offlinebuffer, options) {
	const { onMessage, onOpen, onClose, onError, onPing, onPingTimeout } = options;
	const ws = new WebSocket(url);
	let pingIntervalId;
	let pingTimeoutId;
	ws.onopen = function onopen(event) {
		if (typeof onOpen === 'function') {
			onOpen(event);
		}
		// copy data in local array since offline buffer may be used in this process
		// in case we loose again ou connection
		const localBuffer = offlinebuffer.slice(0);
		offlinebuffer.reset();
		if (localBuffer.length > 0) {
			localBuffer.forEach(msg => wsSend(ws, msg.message, msg.callback, offlinebuffer));
		}
		ws.ping();
		pingIntervalId = setInterval(ws.ping, options.pingInterval || 50000);
	};
	ws.onmessage = function onmessage(messageEvent) {
		if (typeof onMessage === 'function') {
			onMessage(messageEvent);
		}
	};
	ws.onclose = function onclose(closeEvent) {
		if (typeof onClose === 'function') {
			onClose(closeEvent);
		}
		if (pingIntervalId) {
			clearInterval(pingIntervalId);
		}
		if (pingTimeoutId) {
			clearTimeout(pingTimeoutId);
		}
	};
	ws.onerror = function onerror(event) {
		if (typeof onError === 'function') {
			onError(event);
		}
	};
	ws.ping = function ping() {
		if (typeof onPing === 'function') {
			if (!isNaN(options.pingTimeoutDelay)) {
				pingTimeoutId = setTimeout(ws.onpingtimeout, options.pingTimeoutDelay);
			}
			onPing({ pingTimeoutId });
		}
		ws.send('{"type":"PING"}');
	};
	ws.onpingtimeout = function onpingtimeout() {
		if (typeof onPingTimeout === 'function') {
			onPingTimeout();
		}
		ws.close();
	};

	function wsStillNotYetOpened() {
		if (ws.readyState === WebSocket.CONNECTING) {
			ws.onpingtimeout();
		}
	}

	if (!isNaN(options.pingTimeoutDelay)) {
		setTimeout(wsStillNotYetOpened, options.pingTimeoutDelay);
	}

	return ws;
}
/**
 * initialise a websocket
 * on open connection, try to send messages stored in offline buffer
 */
initialise a websocket
on open connection, try to send messages stored in offline buffer
initialise a websocket on open connection try to send messages stored in offline buffer
function startWebsocket(url, offlinebuffer, options) {
	const { onMessage, onOpen, onClose, onError, onPing, onPingTimeout } = options;
	const ws = new WebSocket(url);
	let pingIntervalId;
	let pingTimeoutId;
	ws.onopen = function onopen(event) {
		if (typeof onOpen === 'function') {
			onOpen(event);
		}
		const localBuffer = offlinebuffer.slice(0);
		offlinebuffer.reset();
		if (localBuffer.length > 0) {
			localBuffer.forEach(msg => wsSend(ws, msg.message, msg.callback, offlinebuffer));
		}
		ws.ping();
		pingIntervalId = setInterval(ws.ping, options.pingInterval || 50000);
	};
	ws.onmessage = function onmessage(messageEvent) {
		if (typeof onMessage === 'function') {
			onMessage(messageEvent);
		}
	};
	ws.onclose = function onclose(closeEvent) {
		if (typeof onClose === 'function') {
			onClose(closeEvent);
		}
		if (pingIntervalId) {
			clearInterval(pingIntervalId);
		}
		if (pingTimeoutId) {
			clearTimeout(pingTimeoutId);
		}
	};
	ws.onerror = function onerror(event) {
		if (typeof onError === 'function') {
			onError(event);
		}
	};
	ws.ping = function ping() {
		if (typeof onPing === 'function') {
			if (!isNaN(options.pingTimeoutDelay)) {
				pingTimeoutId = setTimeout(ws.onpingtimeout, options.pingTimeoutDelay);
			}
			onPing({ pingTimeoutId });
		}
		ws.send('{"type":"PING"}');
	};
	ws.onpingtimeout = function onpingtimeout() {
		if (typeof onPingTimeout === 'function') {
			onPingTimeout();
		}
		ws.close();
	};
	function wsStillNotYetOpened() {
		if (ws.readyState === WebSocket.CONNECTING) {
			ws.onpingtimeout();
		}
	}
	if (!isNaN(options.pingTimeoutDelay)) {
		setTimeout(wsStillNotYetOpened, options.pingTimeoutDelay);
	}
	return ws;
}
function startWebsocket ( url , offlinebuffer , options ) { const { onMessage , onOpen , onClose , onError , onPing , onPingTimeout } = options ; const ws = new WebSocket ( url ) ; let pingIntervalId ; let pingTimeoutId ; ws . onopen = function onopen ( event ) { if ( typeof onOpen === 'function' ) { onOpen ( event ) ; } const localBuffer = offlinebuffer . slice ( 0 ) ; offlinebuffer . reset ( ) ; if ( localBuffer . length > 0 ) { localBuffer . forEach ( msg => wsSend ( ws , msg . message , msg . callback , offlinebuffer ) ) ; } ws . ping ( ) ; pingIntervalId = setInterval ( ws . ping , options . pingInterval || 50000 ) ; } ; ws . onmessage = function onmessage ( messageEvent ) { if ( typeof onMessage === 'function' ) { onMessage ( messageEvent ) ; } } ; ws . onclose = function onclose ( closeEvent ) { if ( typeof onClose === 'function' ) { onClose ( closeEvent ) ; } if ( pingIntervalId ) { clearInterval ( pingIntervalId ) ; } if ( pingTimeoutId ) { clearTimeout ( pingTimeoutId ) ; } } ; ws . onerror = function onerror ( event ) { if ( typeof onError === 'function' ) { onError ( event ) ; } } ; ws . ping = function ping ( ) { if ( typeof onPing === 'function' ) { if ( ! isNaN ( options . pingTimeoutDelay ) ) { pingTimeoutId = setTimeout ( ws . onpingtimeout , options . pingTimeoutDelay ) ; } onPing ( { pingTimeoutId } ) ; } ws . send ( '{"type":"PING"}' ) ; } ; ws . onpingtimeout = function onpingtimeout ( ) { if ( typeof onPingTimeout === 'function' ) { onPingTimeout ( ) ; } ws . close ( ) ; } ; function wsStillNotYetOpened ( ) { if ( ws . readyState === WebSocket . CONNECTING ) { ws . onpingtimeout ( ) ; } } if ( ! isNaN ( options . pingTimeoutDelay ) ) { setTimeout ( wsStillNotYetOpened , options . pingTimeoutDelay ) ; } return ws ; }
initialise a websocket
 on open connection, try to send messages stored in offline buffer
initialise a websocket on open connection try to send messages stored in offline buffer
// copy data in local array since offline buffer may be used in this process // in case we loose again ou connection