J
J
jslby2020-04-17 23:03:47
JavaScript
jslby, 2020-04-17 23:03:47

How to connect to an existing Websocket?

There is a site which is written on React + obfurscator
The site establishes connection through websocket.
Can I send requests and receive responses from it without knowing the variable that created this connection? Perhaps it is generally created through an anonymous function. Just wondering if it's real or not? If so, can you show an example?

PS I do not want to create a new connection for a reason, because a bunch of tokens are sent there when connected, as well as maintaining the connection.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Confy, 2020-04-18
@jslby

You need to reassign the built-in websocket class.
An example of working code that should be applied to the page before opening the websocket is attached below.
If the websocket opens on the page right away, you'll need an extension like Tampermonkey to inject the script as early as possible.

window.WebSocketCopy = window.WebSocket;
window.WebSocket = function(a,b,c) { // Притворяемся WebSocket
  // В a,b,c - аргументы, которые обычно передаются в экземпляр класса
  // Пример: a = "ws://35.26.64.245";

  // Создаём реальный экземпляр
  const  socketInstance = new WebSocketCopy(a,b,c);

  // Дальше делаем всё что нам нужно с инстанцией вэбсокета
  socketInstance.addEventListener('message', function (event) {
    console.log(event);
  });

  // И возвращаем обработанный результат
  return socketInstance;
}

I heard somewhere that reassigning built-in classes can affect the performance of the code, so I advise you to use this trick only for debugging.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question