Advertisement
Guest User

Untitled

a guest
May 7th, 2016
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _WebSocket = window.WebSocket;
  2.  
  3. function refer(master, slave, prop) {
  4.     Object.defineProperty(master, prop, {
  5.         get: function(){
  6.             return slave[prop];
  7.         },
  8.         set: function(val) {
  9.             slave[prop] = val;
  10.         },
  11.         enumerable: true,
  12.         configurable: true
  13.     });
  14. };
  15.  
  16. window.WebSocket = function(url, protocols) {
  17.     console.log('Listen');
  18.  
  19.     if (protocols === undefined) {
  20.         protocols = [];
  21.     }
  22.  
  23.     var ws = new _WebSocket(url, protocols);
  24.  
  25.     refer(this, ws, 'binaryType');
  26.     refer(this, ws, 'bufferedAmount');
  27.     refer(this, ws, 'extensions');
  28.     refer(this, ws, 'protocol');
  29.     refer(this, ws, 'readyState');
  30.     refer(this, ws, 'url');
  31.  
  32.     this.send = function(data) {
  33.         // sniff here
  34.         return ws.send.call(ws, data);
  35.     };
  36.  
  37.     this.close = function() {
  38.         return ws.close.call(ws);
  39.     };
  40.  
  41.     this.onopen = function(event) {};
  42.     this.onclose = function(event) {};
  43.     this.onerror = function(event) {};
  44.     this.onmessage = function(event) {};
  45.  
  46.     ws.onopen = function(event) {
  47.         console.log(url);
  48.         if (this.onopen)
  49.             return this.onopen.call(ws, event);
  50.     }.bind(this);
  51.  
  52.     ws.onmessage = function(event) {
  53.         // sniff here (message = event.data)
  54.         if (this.onmessage)
  55.             return this.onmessage.call(ws, event);
  56.     }.bind(this);
  57.  
  58.     ws.onclose = function(event) {
  59.         if (this.onclose)
  60.             return this.onclose.call(ws, event);
  61.     }.bind(this);
  62.  
  63.     ws.onerror = function(event) {
  64.         if (this.onerror)
  65.             return this.onerror.call(ws, event);
  66.     }.bind(this);
  67. };
  68.  
  69. window.WebSocket.prototype = _WebSocket;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement