Monday, August 3, 2015

How to see websocket traffic in Chrome and Fiddler

Websocket是HTML5的一个重要功能,它让server和client端建立新的沟通channel, 让推送的功能更加快速。由于其省去每次发header这个步骤,测试表明websocket比Restful API要快三倍以上。不少网站已经开始使用websocket,比如six flags


这个网站可以测试websocket: https://www.websocket.org/echo.html

我们可以两个方法去看数据:
1. Google Chrome
Chrome在network tab可以看到这个websocket,frames可以看到data






2. Fiddler
在4.5版本中,可以加入额外的代码到custom js中:Rules > Customize Rules and add the following function inside your Handlers class,使得websocket数据显示在Log中:

static function OnWebSocketMessage(oMsg: WebSocketMessage) {
    // Log Message to the LOG tab
    FiddlerApplication.Log.LogString(oMsg.ToString());

    /*
    // Modify a message's content
    var sPayload = oMsg.PayloadAsString();
    if (sPayload.Contains("time")) {
        oMsg.SetPayload(sPayload + "... bazinga!");
    }
                          
    */
  }