获得网站的活跃Websockets吗? [英] Get active Websockets of a Website possible?

查看:42
本文介绍了获得网站的活跃Websockets吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能获得网站的活动WebSocket.一个示例是: var x = document.findWebSocket().该网络套接字将在Chrome的网络"标签下(在开发工具"部分中)列出.从那里开始,Websocket列在"WS"下.我也希望能够做 x.emit(..); .

I would like to know if its possible to get active WebSockets of a Website. An example would be: var x = document.findWebSocket(). The websockets would be listed in Chrome under the Network Tab (In the dev tools section). From there the websockets are listed under "WS". I want to be able to do x.emit(..); as well.

到目前为止,我只能提出 var x = new WebSocket("wss://exampleUrl.com/socket.io/?EIO = 3& transport = websocket","protocol1"); .但这只会添加一个新的Websocket,其sid与我要从中发出消息的sid不同.

So far i could only come up with var x = new WebSocket("wss://exampleUrl.com/socket.io/?EIO=3&transport=websocket", "protocol1");. But this only adds a new Websocket with a different sid from the one that i want to emit messages from.

添加& sid = {Active Websocket的SID}"无效.

adding "&sid = {SID of Active Websocket}" would not work.

推荐答案

有点麻烦,但是如果您可以注入在站点代码执行之前运行的代码(例如,使用Tampermonkey和 @ run-at文档-start ),则可以对 window.WebSocket 进行monkeypatch,以便在调用它时将创建的websocket添加到一个数组中,以便以后检查.例如,在堆栈溢出上运行以下命令:

It's a bit hacky, but if you can inject code that runs before the site's code does (for example, with Tampermonkey and @run-at document-start), you can monkeypatch window.WebSocket so that whenever it's called, you add the created websocket to an array which you can examine later. For example, running the following on Stack Overflow:

// ==UserScript==
// @name             0 New Userscript
// @include          /^https://stackoverflow.com
// @run-at           document-start
// @grant            none
// ==/UserScript==

const sockets = [];
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
  const socket = new nativeWebSocket(...args);
  sockets.push(socket);
  return socket;
};
setTimeout(() => {
  // or create a button which, when clicked, does something with the sockets
  console.log(sockets);
}, 1000);

导致 [WebSocket] 被记录(您可以继续执行实例所需的任何操作,例如调用 emit ).

results in [WebSocket] being logged (and you could proceed to do whatever you wanted to do with the instance, such as call emit).

这篇关于获得网站的活跃Websockets吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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