I’ve got an implementation of SignalR which is being used as a chat client. The problem seems to be that I have a client side timer, that is pinging the server to notify all other clients of a user’s status.
In this instance, I have a heartbeat and a latestactivity. The heartbeat hits the server every fifteen seconds or so, and the latestactivity tracks the user’s interactivity. These two values are passed to the server so that all other clients can be notified of everyone else’s status. Ie: If User A hasn’t moved their mouse for over a minute, when their heartbeat hits the server, it’ll broadcast to every other user via SignalR that they are now ‘away’..
Anyways, I have an issue where there seems to be an exponential increase in SignalR connections the greater the number of user’s that connect to the chat app.
This is the source of the client js timer that I suspect is fishy but I’m not sure whY:
define(['jquery', 'underscore', 'backbone'],
function ($, _, Backbone) {
var Timer = Backbone.Model.extend({
defaults: {
interval: 1 * 10 * 1000,
timeout: null
},
initialize: function (options) {
_.bindAll(this, 'start', 'tick', 'stop', 'tickNow');
if (options.interval) {
this.set('interval', options.interval);
}
},
start: function () {
var timer = setTimeout(this.tick, this.get('interval'));
this.set('timeout', timer);
},
tick: function () {
var self = this;
self.trigger('timerexpired', this);
self.start();
},
tickNow: function () {
var self = this;
self.stop();
self.trigger('timerexpired', this);
self.start();
},
stop: function () {
clearTimeout(this.get('timeout'));
}
});
return Timer;
});
I notice that you are using the
thiskeyword in the start timer method. It could be that your call tothis.tickcall is out of context when it actually fires. Try:In your browser, chuck a breakpoint into the
tickmethod and check that you are indeed referencing the right timer.