Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SignalR, is it possible to simulate a reconnect for testing?

I understand that SignalR continually attempts to reconnect if a connection fails for any reason. What's the best way to test my server and client's response to this?

like image 923
izb Avatar asked Jan 30 '13 15:01

izb


People also ask

Is SignalR bidirectional?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.

How many users can SignalR handle?

Since a browser is limited to six connections, we can try to manually monitor how many connections are supported by a given browser.

How do I stop SignalR connection?

hub. stop(). done(function() { alert('stopped'); });

What technology does SignalR use?

SignalR takes advantage of WebSocket, an HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fall back to other techniques and technologies when it isn't, while the application code remains the same.


2 Answers

In order for the Client to try to reconnect, you need to configure the HubConnectionBuilder with the withAutomaticReconnect method. The JavaScript code looks something like this:

const connection = new signalR.HubConnectionBuilder()
  .withUrl(`${apiUrl}/subscribe/change`)
  .withAutomaticReconnect()
  .build();

Once that is set, the client will try to reconnect when the connection is lost.

Internally, when SignalR is disconnecting, it calls the stopInternal function.

If you want to do some manual testing to make sure that your handlers are being fired and they do what you expect them to do. You can save the connection in a const and fire the events yourself.

What I did:

  1. Created the connection like in the code above,
  2. Cached the connection in the window object.
    window.signalR = connection;
  1. Opened the browser console, clicked the three dots on the left and navigated to Network conditions.

enter image description here

  1. Updated Network throttling to offline.

enter image description here

  1. And finally ran:
signalR.connection.stopInternal();

And my onreconnecting handler started firing. It runs 4 times by default. It looks like this:

connection.onreconnecting(error => {
  console.log('Reconnecting interval', error);
});

NOTE: This strategy is for development and some manual testing only. I wouldn't recommend using the window object for unit tests or integration tests.

A similar strategy can be achieved by caching your connection on some internal variables to be used by unit tests.

like image 98
MauricioLeal Avatar answered Oct 21 '22 05:10

MauricioLeal


I found a solution here: https://stackoverflow.com/a/59916046/2929675

Before the WebSocket connection gets opened, you must execute this script to monkey patch the browsers WebSocket function:

const sockets = [];
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
  const socket = new nativeWebSocket(...args);
  sockets.push(socket);
  return socket;
};

Now you have access to all WebSocket connections via the sockets array. And when you want to trigger a reconnect, you can call

sockets[0].close();

using the index of the connection you want to close.

When SignalR automatic reconnect is enabled, it will automatically try to open a new connection, and the Reconnecting and Reconnected events get fired.

It's a bit hacky, but it's the best solution I have found so far.

It looks like there is also some progress in the chromium issue to support this in the dev tools: https://bugs.chromium.org/p/chromium/issues/detail?id=423246

like image 26
berhir Avatar answered Oct 21 '22 04:10

berhir