是否有一种方法或扩展,让我观察“流量”通过WebSocket?出于调试目的,我希望看到客户端和服务器的请求/响应。


当前回答

Chrome 29及以上版本的简短回答:

打开调试器,转到“网络”选项卡 使用websocket加载页面 单击带有服务器升级响应的websocket请求 选择标签“帧”查看websocket帧 再次单击websocket请求刷新帧

其他回答

我使用的Chrome扩展名为Simple WebSocket Client v0.1.3,是由用户hakobera发布的。它的使用非常简单,它允许在给定的URL上打开websocket,发送消息并关闭socket连接。它非常简约。

你有3个选项:Chrome(通过开发人员工具->网络选项卡),Wireshark和Fiddler(通过日志选项卡),但他们都非常基本。如果你有非常大的流量,或者每帧都非常大,使用它们进行调试就变得非常困难。

然而,你可以使用Fiddler和FiddlerScript来检查WebSocket流量,就像检查HTTP流量一样。这个解决方案的优点是你可以利用Fiddler中的许多其他功能,比如多个检查器(HexView, JSON, SyntaxView),比较数据包,查找数据包等。

请参考我最近在CodeProject上写的文章,这篇文章向你展示了如何用Fiddler(用FiddlerScript)调试/检查WebSocket流量。http://www.codeproject.com/Articles/718660/Debug-Inspect-WebSocket-traffic-with-Fiddler

如果你想要更好的体验,我建议使用Postman来调试WebSocket请求。它已经作为一个新功能发布。 https://stackoverflow.com/a/43754722/15988851

Chrome Canary和Chromium现在有WebSocket消息帧检查功能。下面是快速测试的步骤:

Navigate to the WebSocket Echo demo [dead as of 2022], hosted on the websocket.org site https://echo.websocket.events/.ws (run by the company Lob) or you can locally run the Echo Server project. Turn on the Chrome Developer Tools. Click Network, and to filter the traffic shown by the Dev Tools, click WebSockets. In the Echo demo, click Connect. On the Headers tab in Google Dev Tool you can inspect the WebSocket handshake. Click the Send button in the Echo demo. THIS STEP IS IMPORTANT: To see the WebSocket frames in the Chrome Developer Tools, under Name/Path, click the echo.websocket.org entry, representing your WebSocket connection. This refreshes the main panel on the right and makes the WebSocket Frames tab show up with the actual WebSocket message content.

注意:每次发送或接收新消息时,您都必须通过单击左侧的echo.websocket.org条目来刷新主面板。

我还发布了这些步骤的屏幕截图和视频。

我最近出版的书,The Definitive Guide to HTML5 WebSocket,也有一个专门的附录,涵盖了各种检查工具,包括Chrome Dev tools, Chrome net-internals和Wire Shark。

他们似乎在不断地改变Chrome中的东西,但这里是目前有效的:-)

首先你必须点击红色记录按钮,否则你什么也得不到。 我以前从未注意到WS,但它过滤掉了web套接字 连接。 选择它,然后你可以看到帧(现在称为消息),它将向你显示错误消息等。