سؤال

Is there any way to stop polling server in SignalR? I want to stop polling server if error occurs. Polling hubs is started with $.connection.hub.start(), so I assumed that it could be stopped with $.connection.hub.stop() or something like that. But it doesn't seem to work, polling continues even after calling stop(). Is there another way to stop pollling?

<script type="text/javascript">

 var chatHub = $.connection.chatHub;
 var connection = $.connection.hub;

chatHub.addMessage = function (message) {
      $('#messages').append('<li>' + message + '</li>');
    };

connection.error(function (error) {
 $('#messages').append('<li>' + "error connecting: closing connnection" + '</li>');
    connection.stop(); //This doesn't seem to work   
        });

connection.start();

</script>
هل كانت مفيدة؟

المحلول

If you're using hubs you can stop the hub's connection:

$.connection.hub.stop();

نصائح أخرى

Firstly are you sure that is it necessary? If communication error occurs than obviously you want to retry it.

However, I looked into the source code and I mean that especially in error handler it is not possible call .stop() command. Respectively it has no effect because after handler function is finished than the communication is automatically reinitialized two second after.

See jquery.signalR.js (line 340):

$(instance).trigger("onError", [data]);

window.setTimeout(function () {
  poll(instance);
}, 2 * 1000);

I found one workaround: In handler function throw an exception, which cause that poll is not reinitialized:

connection.error(function (error) { //$.connection.hub.error()
  $('#messages').append('<li>' + "error connecting: closing connnection" + '</li>');
  throw "Close SignalR connection";
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top