如何覆盖 WebSocket send() 方法 [英] How to override WebSocket send() method

查看:52
本文介绍了如何覆盖 WebSocket send() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖 WebSocket.send() 方法.我的目标是能够对发送的数据进行模糊测试,以测试服务器实现的稳健性.

I'm trying to override the WebSocket.send() method. My objective is being able to fuzz the sent data to test robustness of the server implementation.

我正在采用这种方法:

// OVERRIDE WEBSOCKET SEND METHOD
WebSocket.prototype.oldSend = WebSocket.prototype.send;

WebSocket.prototype.send = function(data) {
     console.log("ws: sending data");
     WebSocket.prototype.oldSend(data);
};

当第一次调用 WebSocket.prototype.oldSend(data) 命令时失败,并出现错误:发送失败:在未实现接口 WebSocket 的对象上调用了发送".

This fails when the WebSocket.prototype.oldSend(data) command is called the first time, with the error: Failed to send: 'send' called on an object that does not implement interface WebSocket.

任何人是否可以覆盖内置的 websocket 发送方法(),或者我还缺少什么?

Anybody if it is possible to override the built-in websocket send method(), or what else I'm missing?

TIA

推荐答案

怎么样:

WebSocket.prototype.oldSend = WebSocket.prototype.send;

WebSocket.prototype.send = function(data) {
     console.log("ws: sending data");
     WebSocket.prototype.oldSend.apply(this, [data]);
};

JSFiddle

这篇关于如何覆盖 WebSocket send() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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