Browsers have a limitation of the maximum number of connections to the same host address (same application). So, this limits the maximum number of opened browser windows of the same application with SignalR connections. IWC-SignalR allows to bypass this restriction using single SignalR connection for all windows of the same application. IWC-SignalR was tested with SignalR v2.2.0.
One of the windows becomes a connection owner (choosen randomly) and holds the real SignalR connection. If connection owner is closed or crashed another window becomes a connection owner - this happens automatically. Inter-window communication is done by means of IWC.
To use IWC-SignalR include signalr-patch.js and iwc-signalr.js files on your page. signalr-patch.js allows to start SignalR multiple times. This allows to reconfigure hub proxies at any time.
IWC-SignalR works with auto generated proxies. So, you should also include a reference <script src="signalr/hubs"></script>
before signalr-patch.js and iwc-signalr.js. For example:
<!--Reference the SignalR library. -->
<script src=".../jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<script src=".../iwc-all.js"></script>
<script src=".../signalr-patch.js"></script>
<script src=".../iwc-signalr.js"></script>
Let's have a hub Echo
with method Send
defined on server. Method Send
calls method displayMsg
of all clients.
var echoHub = SJ.iwc.SignalR.getHubProxy('echo', {
client: {
displayMsg: function (msg) {
console.log(msg);
}
}
});
SJ.iwc.SignalR.start().done(function () {
console.log('started');
echoHub.server.send('test').done(function () {
console.log('sent');
});
});
//Result in console:
//started
//test
//sent
IWC-SignalR depends on IWC
SJ.iwc.SignalR.start()
This function has the same signature as the original $.connection.hub.start
function
SJ.iwc.SignalR.start
when called it tries to find connection owner and connect to it via IWC.
If connection owner not found, real SignalR connection is established and this window becomes a connection owner.
{object}SJ.iwc.SignalR.getHubProxy({string}hubName, {object}proxyConfig)
hubName
- hub name according to SignalR naming convention (starts from lower case letter)proxyConfig
- object which contains definition of client's methods
Returns hub proxy object with server and client
Creates hub proxy object with server and client. Usage of server and client doesn't depend on whether the SignalR connection is real or via IWC.
var echoHub = SJ.iwc.SignalR.getHubProxy('echo', {
client: {
displayMsg: function (msg) {
console.log(msg);
}
}
});
echoHub.server.send('test');
{bool}SJ.iwc.SignalR.isConnectionOwner()
Returns true if this window is the owner of SignalR connection
{string}SJ.iwc.SignalR.getConnectionOwnerWindowId()
Returns the ID of connection owner window. Returns null if connection owner not found.
This method works properly only if SJ.iwc.WindowMonitor
is initialized.
SJ.iwc.SignalR.getState()
Returns connection state (see SignalR's $.connection.connectionState
for available values)
Returns the state of real SignalR connection
SJ.iwc.SignalR.getConnectionId()
Returns connection id (see SignalR's $.connection.hub.id
)
Returns the connection id of real SignalR connection
Use SJ.iwc.SignalR.on()
and SJ.iwc.SignalR.un()
to subscribe/unsubscribe on events
statechanged(newState, prevState)
- fired when connection state is changed.newState
,prevState
- new and previous state (see SignalR's$.connection.connectionState
for available values)connected
- fired when state is changed to$.connection.connectionState.connected
starting
,received
,connectionslow
,reconnecting
,reconnected
,disconnected
- are the same events as for original SignalR connection
var someObject = {
onStateChanged: function (newState, prevState) {
}
};
//subscribe
SJ.iwc.SignalR.on('statechanged', someObject.onStateChanged, someObject);
//unsubscribe
SJ.iwc.SignalR.un('statechanged', someObject.onStateChanged, someObject);