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:
The WebSocket. close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing.
If Grok is telling you that WebSockets are blocked, this usually happens because your school or device is blocking WebSockets.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With