Websocket
[VSoRC/.git] / node_modules / websocket / lib / browser.js
1 var _global = (function () {
2         if (!this && typeof global !== 'undefined') {
3                 return global;
4         }
5         return this;
6 })();
7 var NativeWebSocket = _global.WebSocket || _global.MozWebSocket;
8 var websocket_version = require('./version');
9
10
11 /**
12  * Expose a W3C WebSocket class with just one or two arguments.
13  */
14 function W3CWebSocket(uri, protocols) {
15         var native_instance;
16
17         if (protocols) {
18                 native_instance = new NativeWebSocket(uri, protocols);
19         }
20         else {
21                 native_instance = new NativeWebSocket(uri);
22         }
23
24         /**
25          * 'native_instance' is an instance of nativeWebSocket (the browser's WebSocket
26          * class). Since it is an Object it will be returned as it is when creating an
27          * instance of W3CWebSocket via 'new W3CWebSocket()'.
28          *
29          * ECMAScript 5: http://bclary.com/2004/11/07/#a-13.2.2
30          */
31         return native_instance;
32 }
33 if (NativeWebSocket) {
34         ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function(prop) {
35                 Object.defineProperty(W3CWebSocket, prop, {
36                         get: function() { return NativeWebSocket[prop]; }
37                 });
38         });
39 }
40
41 /**
42  * Module exports.
43  */
44 module.exports = {
45     'w3cwebsocket' : NativeWebSocket ? W3CWebSocket : null,
46     'version'      : websocket_version
47 };