是否可以在本地服务器环境中暂时禁用websockets连接? [英] Is it possible to temporarily disable a websockets connection in a local server environment?

查看:114
本文介绍了是否可以在本地服务器环境中暂时禁用websockets连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用websockets(socket.io)的Node.js脚本.该脚本正在我的计算机(例如本地服务器环境)上运行,并且我还通过计算机上的浏览器进行测试.我试图优雅地处理断开连接和重新连接.到目前为止,我已经通过在iPad上禁用/启用WiFi来模拟网络干扰.是否可以在本地计算机上通过浏览器插件,第三方代理软件或某些其他工具来禁用和恢复浏览器的Web套接字连接?

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?

我尝试过的票价:

  • Chrome开发者工具:在其设备模式中,有网络条件 此时,它仅适用于HTTP连接,不适用于websocket连接.
  • 查尔斯:它记录了websocket连接,但不限制它们.(我已将带宽,利用率和MTU设置为零,但仍通过Websocket连接接收到响应.)
  • Fiddler :类似地记录websocket连接,但不阻止它们
  • 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.

推荐答案

您可以尝试重载WebSocket对象.它有点脏,但是您可以在尝试创建websocket时捕获并创建延迟或其他原因.我还没有测试过,但应该是正确的.或者,可能在发送或接收数据时使WS方法本身过载,从而中断.

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;

这篇关于是否可以在本地服务器环境中暂时禁用websockets连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆