Skip to content

Instantly share code, notes, and snippets.

@yagitoshiro
Created January 18, 2018 03:14
Show Gist options
  • Save yagitoshiro/c295479e33db6372f6c4271d4d1cb99a to your computer and use it in GitHub Desktop.
Save yagitoshiro/c295479e33db6372f6c4271d4d1cb99a to your computer and use it in GitHub Desktop.
websocket
//ブラウザ全体でWebsocketを落としたらどうなるか試験するときに使う
// https://stackoverflow.com/a/31526291/5518615
var WebSocket2 = WebSocket;
WebSocket = function(addy) {
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.close = function(){};
WebSocket.prototype.blocked = false;
//WebSocket.toggle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment