socketcluster-client
Version:
SocketCluster JavaScript client
36 lines (30 loc) • 925 B
JavaScript
let globalScope;
if (typeof WorkerGlobalScope !== 'undefined') {
globalScope = self;
} else {
globalScope = typeof window !== 'undefined' && window || (function() { return this; })();
}
const WebSocket = globalScope.WebSocket || globalScope.MozWebSocket;
/**
* WebSocket constructor.
*
* The third `opts` options object gets ignored in web browsers, since it's
* non-standard, and throws a TypeError if passed to the constructor.
* See: https://github.com/einaros/ws/issues/227
*
* @param {String} uri
* @param {Array} protocols (optional)
* @param {Object} opts (optional)
* @api public
*/
function ws(uri, protocols, opts) {
let instance;
if (protocols) {
instance = new WebSocket(uri, protocols);
} else {
instance = new WebSocket(uri);
}
return instance;
}
if (WebSocket) ws.prototype = WebSocket.prototype;
module.exports = WebSocket ? ws : null;