browser.js 1.1 KB

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