Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to temporarily disable a websockets connection in a local server environment?

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.
like image 563
bonna Avatar asked Jun 18 '15 10:06

bonna


People also ask

Can server close WebSocket connection?

The WebSocket. close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing.

Can WebSockets be blocked?

If Grok is telling you that WebSockets are blocked, this usually happens because your school or device is blocking WebSockets.

How do I turn off WebSockets in Chrome?

You can close idle and flush your inactive socket pools in the Net Internals page in Chrome. However, this unfortunately only closes your active sockets by the looks of it. You would have to use the WebSockets API and call close() on a reference to an existing socket to close it explicitly.

Can WebSockets be intercepted?

To intercept the messages, you will have to spy on the onmessage = fn and addEventListener("message", fn) calls. To be able to modify the onmessage we have to override the global WebSocket in the first place.


1 Answers

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;
like image 182
user3661841 Avatar answered Oct 08 '22 19:10

user3661841