Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Phantom.js documentation shows how to monitor HTTP communication: http://phantomjs.org/network-monitoring.html

However, it does not work for WebSockets. How can I monitor WebSocket communication in Phantom.js?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
706 views
Welcome To Ask or Share your Answers For Others

1 Answer

PhantomJS 1.x does not support WebSockets1, so you cannot monitor them. If the page uses some fallback for WebSockets, then the page.onResourceRequested and page.onResourceReceived can be used to log the meta data of the messages. PhantomJS does not expose the actual data sent in any way.

WebSockets work correctly in PhantomJS 2. Since no fallback is necessary, it is not possible to observe the traffic with those events. The event handlers mentioned above don't show anything. The only way to see the messages would be to proxy the WebSocket object as early as possible:

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));
};

1 My tests actually show that the websocket echo page works with v1.9.6 and up, but not v1.9.0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...