admin管理员组

文章数量:1278376

I have a Node.js script that uses websockets (socket.io). The script is running on my computer (e.g. local server environment) and I also tests through a browsers on my computer. I am trying to gracefully handle disconnections and reconnections. So far I have emulated network disturbances by disable/enable the WiFi on my iPad. Is it possible to disable and resume the browser's web socket connection on my local machine, i.e. through a browser plugin, third party proxy software, or some other tool?


So fare I have tried:

  • Chrome developer tool: In its Device Mode there is a Networks Conditions option, which allows me to emulate various network connectivity. Unfortunately at this point in time it only applies to HTTP-connections and not to the websocket connections.
  • Charles: It records the websocket connections, but does not throttle them. (I've set bandwidth, utilisation and MTU to zero, but still receive responses trough the websocket connection).
  • Fiddler: Similarly records the websocket connections, but does not block them.

I have a Node.js script that uses websockets (socket.io). The script is running on my computer (e.g. local server environment) and I also tests through a browsers on my computer. I am trying to gracefully handle disconnections and reconnections. So far I have emulated network disturbances by disable/enable the WiFi on my iPad. Is it possible to disable and resume the browser's web socket connection on my local machine, i.e. through a browser plugin, third party proxy software, or some other tool?


So fare I have tried:

  • Chrome developer tool: In its Device Mode there is a Networks Conditions option, which allows me to emulate various network connectivity. Unfortunately at this point in time it only applies to HTTP-connections and not to the websocket connections.
  • Charles: It records the websocket connections, but does not throttle them. (I've set bandwidth, utilisation and MTU to zero, but still receive responses trough the websocket connection).
  • Fiddler: Similarly records the websocket connections, but does not block them.
Share Improve this question edited Jun 19, 2015 at 14:53 bonna asked Jun 18, 2015 at 10:28 bonnabonna 1,6152 gold badges18 silver badges31 bronze badges 2
  • 1 Very important question. Same problem here. – Skalár Wag Commented Jul 14, 2015 at 15:47
  • in firefox, you can check "Work Offline", but i'm not 100% sure that applies to sockets. you might also be able to use a firewall to block/unblock the port. – dandavis Commented Jul 16, 2015 at 5:11
Add a comment  | 

5 Answers 5

Reset to default 7

You can try overloading the WebSocket object. Its kind of dirty, but you could capture when the websocket is trying to be created and create delays or whatever. I havent tested it, but it should be sound. Or maybe overload the WS methods themselves to interrupt when data is being sent or received.

        var WebSocket2 = WebSocket;

        WebSocket = function(addy) {
            console.log('WS: Trying to open.');
            var ws;
            if (!this.blocked) {
                console.log('WS: Not blocked, allowing.');
                ws = new WebSocket2(addy);
                this.open_sockets.push(ws);
                return ws;
            } else {
                console.log('WS: Blocked.');
            }
        };

        WebSocket.toggle = function() {
            WebSocket.prototype.blocked = !WebSocket.prototype.blocked;
            var sockets = WebSocket.prototype.open_sockets;
            if (WebSocket.prototype.blocked) {
                console.log('WS: Blocking. Removing Old Sockets.');
                sockets.forEach(function(socket, index, sockets) {
                    console.log("WS: Closing -", index);
                    socket.close();
                });
                WebSocket.prototype.open_sockets = [];
                console.log("WS: Sockets left open -", WebSocket.prototype.open_sockets.length);
            } else {
                console.log("WS: Unblocking");
            }
        };

        WebSocket.prototype.open_sockets = [];
        WebSocket.prototype.blocked = false;

After seeing @user3661841's answer above, 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:

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:

You could use a simple tcp proxy like tcp-proxy, and simply close the proxy to terminate the WebSocket connection.

Hijacking the webSocket prototype is not the best solution when you can easily catch websockets on the fly in the request headers.

To catch and prompt the user of incoming websockets...

chrome.webRequest.onBeforeRequest.addListener(function(details){

 let Block=false;

 if(details.type=='websocket'){

  let S='This site is attempting to establish a websocket connection with you!\n\n'+

    'Site: '+details.initiator+'\n\n'+

    'Socket: '+details.url+'\n\n'+

    'Do you want to allow this?';

   Block=!confirm(S);

 }

} return{cancel:Block};},{urls:['<all_urls>']},['blocking']);

To block websockets outright silently...

chrome.webRequest.onBeforeRequest.addListener(function(details){

 let Block=false;

 if(details.type=='websocket'){Block=true;}

} return{cancel:Block};},{urls:['<all_urls>']},['blocking']);

Test sites used in 2024:

https://tass.com/
https://soccer365.me/online/

NB: We can only use the native dialogs to pause incoming websockets because only they can pause JS dead in its tracks.

Execute the browser with special arguments:

chrome.exe --disable-web-sockets

本文标签: