How can i temporarily disable websocket in Google Chrome?

Google ChromeWebsocket

Google Chrome Problem Overview


I want temporarily disable websocket in Google Chrome to debug a javascript to make sure it work for any browser without websocket support.

Google Chrome Solutions


Solution 1 - Google Chrome

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:

WebSocket = undefined;

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).

Solution 2 - Google Chrome

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):

    // Block Websockets
public static RulesOption("Block Websockets")
BindPref("fiddlerscript.rules.BlockWebsockets")
var m_BlockWebsockets: boolean = false;	

Add this code in OnBeforeRequest:

    if (m_BlockWebsockets && oSession.oRequest.headers.Exists("Connection") && oSession.oRequest["Connection"] == "Upgrade") {
    			oSession.oRequest.FailSession(502, "Blocked", "Fiddler blocked websocket connection");
    			return;
    		}

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.

Solution 3 - Google Chrome

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.):

  1. Download the TamperMonkey extension from the Chrome Store
  2. Install this script from GreasyFork. By default, installing this script will disable WebSockets on every site you visit. If you don't want to block WebSockets immediately, click on the TamperMonkey icon and the toggle switch to disable blocking.
  3. When you want to turn off WebSockets, click on the TamperMonkey icon and the toggle switch to enable blocking. Refresh the page.
  4. Disable the script when you no longer want to block WebSockets.

Here's the switch you want to click to disable/enable WebSocket blocking:

enter image description here

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:

Tamper Monkey when not testing

And like this when you want to block:

Tamper Monkey when testing

Solution 4 - Google Chrome

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

enter image description here

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.

enter image description here

Initiator file can be found in network tab enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCyganView Question on Stackoverflow
Solution 1 - Google ChromeMike WestView Answer on Stackoverflow
Solution 2 - Google ChromeDebby MendezView Answer on Stackoverflow
Solution 3 - Google ChromecsalvatoView Answer on Stackoverflow
Solution 4 - Google ChromeMindaugasView Answer on Stackoverflow