通过原型监听WebSocket连接 [英] Listening to a WebSocket connection through prototypes

查看:505
本文介绍了通过原型监听WebSocket连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上花了几个星期,但似乎找不到解决方法.这是用例.我正在开发chrome扩展程序,此扩展程序的主要目的之一是检测WebSocket何时打开或关闭.我看过WebSockets的HTML5规范,它们公开了以下相关方法/事件:

I have spent a couple weeks on this issue and I can’t seem to find a resolution. Here is the use case. I am developing a chrome extension, one of the main purposes of this extension is to detect when a WebSocket opens or closes. I have looked at the HTML5 spec for WebSockets, they expose the following related methods/events:

WebSocket.prototype.readyState = 0;
WebSocket.prototype.onopen = 0;
WebSocket.prototype.onerror = 0;
WebSocket.prototype.onclose = 0;
function WebSocket(url,protocols) {}
WebSocket.prototype.close = function(code,reason) {};

由于我无法从扩展程序内访问单个实例,因此我不得不依靠包装这些原型来确定何时调用它们.例如:

Since I don’t have access to the individual instance from within the extension, I have to rely on wrapping these prototypes to find out when they are called. For example:

WebSocket.prototype.close = function (close) {
    console.log("Closing socket....");
    return function (code, reason) {
        return close.apply(this, arguments);
    };
}(WebSocket.prototype.close);

但是,我真正想做的是以某种方式为onopen,onerror或onclose包装或注册侦听器.不幸的是我不知道该怎么做.我尝试了很多不同的方法.上面的方法适用于关闭",但它们不提供我依赖的打开".最有前途的想法没有用,如下:

However, what I really want to do is somehow wrap or register listeners for onopen, onerror, or onclose. Unfortunately I do not know how to do this. I tried a bunch of different approaches. The approach above works for 'close', but they do not offer 'open', which I am dependent on. The most promising idea, which did not work, was the following:

WebSocket.prototype.constructor = function (constructor) {
    debugger;
    console.log("Register all my events here!");
    constructor.apply(this, arguments);
}(WebSocket.prototype.constructor);

不幸的是,我从来没有碰到过断点,也没有看到控制台日志,这使我相信自己做错了事.

Unfortunately, I was never able to hit a breakpoint, or see console logs, which led me to believe I was doing something wrong.

作为最后的选择,我尝试使用Object.Observe(),但无法观察到原型:

As a last resort, I tried to play around with Object.Observe(), but was unable to observe the prototype:

Object.observe(WebSocket.prototype.readyState, function (changes) {
    console.log("Ready state");
});

Object.observe(WebSocket.prototype.onopen, function (changes) {
    console.log("On open");
});

因此,总而言之,我尝试了几种不同的方法,但最终我碰壁了.最后,我的目标是通过侦听任何公开的方法或事件来检测WebSocket何时打开或关闭.任何帮助将不胜感激.

So, to sum things up, I have tried a few different approaches, but ultimately I have hit a wall. In the end my goal is to detect when a WebSocket opens or closes by listening to any of the exposed methods or events. Any help would be greatly appreciated.

更新: 这是一个小提琴,包括以下建议:

Update: Here is a fiddle, including the suggestions below:

http://jsfiddle.net/s35zpfq9/1/

谢谢!

推荐答案

您可以包装WebSocket来监视切换后创建的所有实例:

you can wrap the WebSocket to spy on all instances created after you switch it:

(function(){

var ws = window.WebSocket;

window.WebSocket = function (a, b) {
   var that = b ? new ws(a, b) : new ws(a);
   that.addEventListener("open", console.info.bind(console, "socket open"));
   that.addEventListener("close", console.info.bind(console, "socket close"));
   that.addEventListener("message", console.info.bind(console, "socket msg"));
   return that;
};

window.WebSocket.prototype=ws.prototype; 

}());

这篇关于通过原型监听WebSocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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