如何在 Google Chrome 中暂时禁用 websocket?
我想暂时禁用 Google Chrome 中的 websocket 来调试 javascript,以确保它适用于任何不支持 websocket 的浏览器。
I want temporarily disable websocket in Google Chrome to debug a javascript to make sure it work for any browser without websocket support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,我想说有更好的机制可以在无 websocket 环境中测试 JavaScript。例如,您可以在 IE9 中运行 JavaScript,但它不实现该协议。如果您更喜欢这种风格,它们目前在 Firefox 4 中仍然处于禁用状态。
假设您有充分的理由需要在无网络套接字的 Chromium 中进行测试,我认为您运气不好。 Chromium 中没有一种简单的机制可以禁用 WebSocket。它不是作为命令行开关内置的,也没有可配置的标志。由于没有机制可以让这种情况在本地发生,我不建议花时间测试该场景。您的用户使用的每个 Chromium 版本(例如 9+)都启用了 Websocket。
话虽如此,如果您确实需要禁用 websockets,那么在不重新编译浏览器的情况下最接近的方法就是删除测试代码中的相关变量:
相对暴力,但应该可行。您甚至可以创建一个扩展,将该 JavaScript 注入到您访问的每个页面中,以获得真正的无 Websocket 体验(再次假设这对您的用例有价值)。
To begin, I'd say that there are better mechanisms to test your JavaScript in a websocketless environment. You could run your JavaScript in IE9, for instance, which doesn't implement the protocol. They're still disabled for the moment in Firefox 4 as well, if that's more your style.
Assuming that there's some good reason that you need to test in a websocketless Chromium, I think you're out of luck. There's not a trivial mechanism to disable WebSockets in Chromium. It's not built in as a command-line switch, nor is there a configurable flag. Since there's no mechanism to make this happen natively, I wouldn't suggest spending time testing the scenario. Every version of Chromium that your users use (e.g. 9+) has websockets enabled.
All that said, if you really need to disable websockets, the closest you can get without recompiling the browser would be to drop the relevant variables in your test code:
would be relatively brute force, but should work. You could even create an extension to inject that JavaScript into every page you visit, for a truly websocketless experience (assuming, again, that that's somehow valuable for your use case).
在看到@user3661841的对另一个问题的回答后,我创建了一个GreaseMonkey/TamperMonkey脚本,它允许您以类似的方式禁用WebSockets方式。
以下是针对基于 Chromium 的浏览器(Chrome、Brave、Sidekick 等)的说明:
以下是您要单击以禁用/启用 WebSocket 阻止的开关:
注意 1:您应该能够在控制台中看到输出,标记 WebSocket 连接已尝试打开,但被阻止。
注意 2:在禁用 WebSocket 进行开发后,请确保禁用此功能。如果您只使用 TamperMonkey 来禁用 WebSocket,您会希望 TamperMonkey 大多数时候看起来像这样:
< img src="https://i.sstatic.net/0ejbL.png" alt="未测试时的 Tamper Monkey">
当您想要阻止时,如下所示:
After seeing @user3661841's answer on another question, I created a GreaseMonkey/TamperMonkey script that will allow you to disable WebSockets in a similar way.
Here's instructions for Chromium based browsers (Chrome, Brave, Sidekick, etc.):
Here's the switch you want to click to disable/enable WebSocket blocking:
NOTE 1: You should be able to see output in your console marking that the WebSocket connection was attempted to be opened, but blocked.
NOTE 2: Make sure to disable this when you're done disabling WebSockets for development. If you only use TamperMonkey to disable WebSockets, You'll want your TamperMonkey to look like this most of the time:
And like this when you want to block:
我能够使用 Fiddler 阻止 WebSocket。
转到 Fiddler 菜单中的 Rules/Customize Rules...
将此代码添加到类 Handlers 中(我将其放在现有 RulesOption 项之后):
在 OnBeforeRequest 中添加此代码:
这将添加一个菜单选项 “阻止 Websocket” 到“规则”菜单。当它打开时,ws 连接应该根据 Connection: Upgrade 标头被阻止。
I was able to block WebSockets with Fiddler.
Go to Rules / Customize Rules... in the Fiddler menu
Add this code to class Handlers (I put it after the existing RulesOption items):
Add this code in OnBeforeRequest:
This adds a menu option Block Websockets to the Rules menu. When it is toggled on, ws connections should be blocked based on the Connection: Upgrade header.
要禁用 ws,您可以简单地在 chrome 开发工具中添加“请求阻止”,但前提是特定文件正在启动 websocket,以防止其加载,如果您不能这样做,则此解决方案可能无法工作
例如:
https://www.websocket.org/echo.html
Websocket连接由echo.js发起,这样你就可以阻止该文件的加载,并且 websocket 将永远不会启动。
启动器文件可以在网络选项卡中找到
To disable ws you can simply add "Request blocking" in chrome dev tools, but only if specific file is initiating websocket to prevent it from loading if you can't do that this solution might not work
For example:
https://www.websocket.org/echo.html
Websocket connection is initiated by echo.js, so you can prevent that file from laoding and websocket will never start.
Initiator file can be found in network tab
这个 Chrome 扩展帮助我禁用了套接字。简单&简单明了。
Github 链接
This Chrome extension helped me in disabling sockets. Simple & Straighforward.
Github Link