2016-05-07 5 views

答えて

0

あなたが新しいものに送信機能を複製して、あなたのログを追加して、元の関数を上書きすることができます。 websocketインスタンスを作成するたびにそれを行う必要があります。

これはあなたがそれを行う方法です。

//First, create a clone prototype 

Function.prototype.clone = function() { 
    var that = this; 
    var temp = function temporary() { return that.apply(this, arguments); }; 
    for(var key in this) { 
     if (this.hasOwnProperty(key)) { 
      temp[key] = this[key]; 
     } 
    } 
    return temp; 
}; 

//Then, when you create your ws object, use the clone function to copy the "send". For example, you can name the new function "parentSend" 

ws = new WebSocket('ws://mystuff.com'); 
ws.parentSend = ws.send.clone(); 

//Finally, recode a new send function, with the logs, and the call to parentsend() 

ws.send = function(msg){ 
    console.log(msg); 
    this.parentSend(msg); 
}; 

これは、私たちはws.sendを呼び出すたび簡単な方法

var ws = new WebSocket("ws://example.com:80"); 
ws.oldSend = ws.send; 
ws.send = function(data) { 
    ws.oldSend(data); 
    console.log('Captured data:', data); 
};` 

は今、それが私たちの再定義WSを呼び出し、それをやった

0

後にログを送信すると、元の関数を呼び出します送信する情報を記録するだけでなく送信する.send。

ws.send('Important data!'); //will show: Captured data: Important data! 
関連する問題