Phantom.js文檔顯示如何監視HTTP通信:http://phantomjs.org/network-monitoring.html如何監控Phantom.js中的WebSocket通訊?
但是,它不適用於WebSockets。我如何監控Phantom.js中的WebSocket通信?
Phantom.js文檔顯示如何監視HTTP通信:http://phantomjs.org/network-monitoring.html如何監控Phantom.js中的WebSocket通訊?
但是,它不適用於WebSockets。我如何監控Phantom.js中的WebSocket通信?
PhantomJS 1.x不支持WebSockets ,所以你不能監控它們。如果頁面對WebSocket使用某種回退,則可以使用page.onResourceRequested
和page.onResourceReceived
來記錄消息的元數據。 PhantomJS不會公開以任何方式發送的實際數據。
的WebSockets正確PhantomJS 2.工作由於沒有回退是必要的,它是不可能觀察這些事件的流量。上面提到的事件處理程序沒有顯示任何內容。看到消息的唯一途徑是代理WebSocket
對象儘早:
page.onInitialized = function(){
page.evaluate(function(){
(function(w){
var oldWS = w.WebSocket;
w.WebSocket = function(uri){
this.ws = new oldWS(uri);
...
};
w.WebSocket.prototype.send = function(msg){
w.callPhantom({type: "ws", sent: "msg"});
this.ws.send(msg);
};
...
})(window);
});
};
page.onCallback = function(data){
console.log(JSON.stringify(data, undefined, 4));
};
我的實際測試表明,websocket echo page作品與v1.9.6及以上,但不v1.9.0。
如果頁面使用了一些備用的的WebSockets,那麼 page.onResourceRequested和page.onResourceReceived可以用來 日誌消息的元數據。 PhantomJS不以任何方式公開實際發送的數據。
Slimer JS幫助解決了這個問題。 Slimer JS會讓你捕捉響應主體。這對於幻影JS 1.x來說是不可能的,並且該功能從最新的2.x版本中刪除。
https://github.com/ariya/phantomjs/issues/10158
使用slimerjs,你可以捕捉XHR請求的內容,但不能用於WebSocket的要求去做。 因此,當頁面初始化時(page.onInitialized),我們將顯式禁用網頁的websockets,以便網頁將使用後備(即XHR),然後您可以捕獲內容。
page.captureContent = [ /application\/javascript/ ];
page.onInitialized = function() {
page.evaluate(function() {
(function(w) {
window.WebSocket = undefined; // we are explicitly disabling websockets for the page. so that the website will be using the fallback
})(window);
});
};
page.open(url, function(status) {
})
你能看到我的問題在http://stackoverflow.com/questions/35453429/is-it-possible-capture-and-log-my-own-websocket-wss-traffic – famedoro