UNPKG

diffusion

Version:

Diffusion JavaScript client

77 lines (71 loc) 2.26 kB
/*eslint valid-jsdoc: "off"*/ var NodeWebSocket = require('ws'); var log = require('util/logger').create('Transports'); /** * Support the detection and creation of WebSocket and XmlHttpRequest objects * in an environment independent way. Each subtransport object provides an * enabled field that indicates if the subtransport works and if it works the * environment specific constructor for the object. */ /** * Optionally environment specific websockets. */ function websocketSubTransportFactoryProvider() { if (typeof WebSocket === 'function') { log.debug('Using native websocket'); return { constructor : WebSocket, enabled : true }; } else if (typeof NodeWebSocket === 'function') { // Browser version does not include 'ws' lib; use native impl log.debug('Using Node WS library'); return { constructor : NodeWebSocket, enabled : true }; } else { log.debug('Websocket transport not available'); return { enabled : false }; } } /** * Optionally provide environment specific XHR. */ function xhrSubTransportFactoryProvider() { if (typeof XMLHttpRequest === 'function') { log.debug('Using native XHR'); return { constructor : XMLHttpRequest, enabled : true }; } else { log.debug('XHR transport not available'); return { enabled : false }; } } /** * The subtransports will be looked up by transport name. */ module.exports = { /** * WebSocket subtransport. Supports detection of availability and construction when available. */ WS : websocketSubTransportFactoryProvider(), /** * HTTP polling subtransport. Supports detection of availability and construction when available. */ XHR : xhrSubTransportFactoryProvider(), /** * WebSocket subtransport. Supports detection of availability and construction when available. */ WEBSOCKET : websocketSubTransportFactoryProvider(), /** * HTTP polling subtransport. Supports detection of availability and construction when available. */ HTTP_POLLING : xhrSubTransportFactoryProvider() };