-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathbrowser.js
50 lines (44 loc) · 1.28 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var _globalThis;
try {
_globalThis = require('es5-ext/global');
} catch (error) {
} finally {
if (!_globalThis && typeof window !== 'undefined') { _globalThis = window; }
if (!_globalThis) { throw new Error('Could not determine global this'); }
}
var NativeWebSocket = _globalThis.WebSocket || _globalThis.MozWebSocket;
var websocket_version = require('./version');
/**
* Expose a W3C WebSocket class with just one or two arguments.
*/
function W3CWebSocket(uri, protocols) {
var native_instance;
if (protocols) {
native_instance = new NativeWebSocket(uri, protocols);
}
else {
native_instance = new NativeWebSocket(uri);
}
/**
* 'native_instance' is an instance of nativeWebSocket (the browser's WebSocket
* class). Since it is an Object it will be returned as it is when creating an
* instance of W3CWebSocket via 'new W3CWebSocket()'.
*
* ECMAScript 5: http://bclary.com/2004/11/07/#a-13.2.2
*/
return native_instance;
}
if (NativeWebSocket) {
['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function(prop) {
Object.defineProperty(W3CWebSocket, prop, {
get: function() { return NativeWebSocket[prop]; }
});
});
}
/**
* Module exports.
*/
module.exports = {
'w3cwebsocket' : NativeWebSocket ? W3CWebSocket : null,
'version' : websocket_version
};