c# - SignalR client method fires multiple times -


i'm playing around signalr, trying create heatmap overlay on google map. however, method firing multiple times , can't figure out why.

the data returned sql formatted json, can plot onto overlay using plugin - http://www.patrick-wied.at/static/heatmapjs/

i've been following web api demo found @ http://techbrij.com/database-change-notifications-asp-net-signalr-sqldependency without luck. code below:

view (snipped show relevant code)

<script type="text/javascript">      var map;     var heatmap;     var testdata;      $(function () {          // proxy created on fly         var sales = $.connection.productsaleshub;          // declare function on product sales hub server can invoke         sales.client.displaysales = function () {             getsalesdata();         };          // start connection         $.connection.hub.start();         getsalesdata();     });      function getsalesdata() {         $.ajax({             url: '../api/values',             type: 'get',             datatype: 'json'         })             .done(function (res) {             if (res.length > 0) {                 var mylatlng = new google.maps.latlng(48.3333, 16.35);                 // sorry - demo beta                 // there lots of work todo                 // don't have enough time eg redrawing on dragrelease right                 var myoptions = {                     zoom: 2,                     center: mylatlng,                     maptypeid: google.maps.maptypeid.satellite,                     disabledefaultui: false,                     scrollwheel: true,                     draggable: true,                     navigationcontrol: true,                     maptypecontrol: false,                     scalecontrol: true,                     disabledoubleclickzoom: false                 };                  testdata = res;                  map = new google.maps.map(document.getelementbyid("heatmaparea"), myoptions);                  heatmap = new heatmapoverlay(map, {                     "radius": 15,                     "visible": true,                     "opacity": 60,                     legend: {                         position: 'br',                         title: 'amount of items sold'                     }                 });                   google.maps.event.addlisteneronce(map, "idle", function() {                     heatmap.setdataset(testdata);                 });             }             })             .fail(function () {                 alert("error");             })             .always(function() {                 alert("complete");             });     }  </script> 

values controller:

public class valuescontroller : apicontroller {      productsalesrepository repo = new productsalesrepository();       // api/values     public jobject get()     {         var data = repo.getproductsalesdata();         return repo.buildjson(data);     }   } 

productsaleshub.cs

public class productsaleshub : hub {     public static void show()     {         ihubcontext context = globalhost.connectionmanager.gethubcontext<productsaleshub>();         context.clients.all.displaysales();     } } 

and lastly, repo

public class productsalesrepository {     public ienumerable<productsalesinfo> getproductsalesdata()     {         using (             var connection =                 new sqlconnection(configurationmanager.connectionstrings["defaultconnection"].connectionstring))         {             connection.open();             using (sqlcommand command = new sqlcommand(@"select top 10 [lat],[lng],[count]            [dbo].[productsales]", connection))             {                 // make sure command object not have                 // notification object associated it.                 command.notification = null;                  sqldependency dependency = new sqldependency(command);                 dependency.onchange += new onchangeeventhandler(dependency_onchange);                  if (connection.state == connectionstate.closed)                     connection.open();                  using (var reader = command.executereader())                     return reader.cast<idatarecord>()                         .select(x => new productsalesinfo()                             {                                 lat = x.getstring(0),                                 long = x.getstring(1),                                 count = x.getint32(2)                             }).tolist();             }         }     }      public jobject buildjson(ienumerable<productsalesinfo> data )     {         ienumerable<productsalesinfo> productsalesinfos = data list<productsalesinfo> ?? data.tolist();         int max = (from d in productsalesinfos.tolist() select d.count).max();          jobject o = new jobject(             new jproperty("max", max),             new jproperty("data",                           new jarray(from d in productsalesinfos                                      select new jobject(                                          new jproperty("lat", d.lat),                                          new jproperty("lng", d.long),                                          new jproperty("count", d.count)))));          return o;     }      private void dependency_onchange(object sender, sqlnotificationeventargs e)     {         productsaleshub.show();     } } 

i've been staring @ hours now, without figuring out why ajax call triggered multiple times.

any ideas?

i had exact same problem. injecting repository class , had multiple instances of it. here adding event every time getproductsalesdata() method called. add event once in constructor , use singleton pattern make sure called once.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -