locked
SignalR clientside receive not working. RRS feed

  • Question

  • User64294366 posted

    Hello Folks - I am looking for a little help with the clientside code on my SignalR project. The jquery function is fired $(function(), and it appears a connection to the hub is established, however the notifications.client.receiveNotification = function does not get executed as an alert() inside the function does not present itself. 

    The server side code is working correctly and values are returned from the query. At the moment I am trying to pass one parameter.

    This project was started from a tutorial and modified to fit my needs.

    Thank-you,

    The client side code:

    <script type="text/javascript">
       
        $(function () {
           // $.connection.notificationHub.logging = true;
           // Declare a proxy to reference the hub.
            var notifications = $.connection.notificationHub;
            alert();
            // Create a function that the hub can call to broadcast messages.
            notifications.client.receiveNotification = function (MLT3) {
                alert(MLT3);
                //$('#lblMLT1').val(MLT1);
                //$('#lblMLT2').val(MLT2);
                $('#lblMLT3').val(MLT3);
                //$('#lblMLT4').val(MLT4);
                //$('#lblMLT5').val(MLT5);
               
    
            };
    
            // Start the connection.
            $.connection.hub.start().done(function () {
                notifications.server.sendNotifications();
            }).fail(function (e) {
                alert(e);
            });
            
        });
    </script>

    Server side Hub

    namespace SignalRNotifications
    {
      //  string query = "SELECT  NewMessageCount, NewCircleRequestCount, NewNotificationCount, NewJobNotificationCount FROM [dbo].[NotificationCount] WHERE UserProfileId=" + "61764";
    
        [HubName("notificationHub")]
        public class NotificationHub : Hub
        {
            Int32 TML1 = 0; Int32 MLT5 = 0;
            Int32 TML2 = 0; Int32 SNL1 = 0;
            Int32 TMT1 = 0; Int32 SNL2 = 0;
            Int32 TMT2 = 0; Int32 SNL3 = 0;
            Int32 MLT1 = 0; Int32 SNL4 = 0;
            Int32 MLT2 = 0; Int32 SNL5 = 0;
            Int32 MLT3 = 0; Int32 IBL1 = 0;
            Int32 MLT4 = 0; Int32 IBL2 = 0;
            Int32 CAV1 = 0; Int32 IBL3 = 0;
            Int32 CAV2 = 0; Int32 IBL4 = 0;
            Int32 CAV3 = 0; Int32 IBL5 = 0;
            Int32 CAV4 = 0; Int32 PAW1 = 0;
            Int32 CAV5 = 0; Int32 PAW2 = 0;
                            Int32 PAW3 = 0;  
            Int32 PAW4 = 0; Int32 PAW5 = 0;
            Int32 PLB1 = 0; Int32 PLB2 = 0;
            Int32 PLB3 = 0; Int32 PLB4 = 0;
            Int32 PLB5 = 0;  
             [HubMethodName("sendNotifications")]
             public void SendNotifications()
            {
                Line ln = new Line();
    
                using ( var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CAVPCConnectionString"].ConnectionString) )
                {
    
    
                    String query = "SELECT TML1,TML2, TMT1, TMT2, MLT1, MLT2, MLT3,MLT4,MLT5,SNL1,SNL2,SNL3,SNL4,SNL5,CAV1,CAV2,CAV3,CAV4,CAV5,IBL1,IBL2,IBL3,IBL4,IBL5,PAW1,PAW2,PAW3,PAW4,PAW5,PLB1,PLB2,PLB3,PLB4,PLB5 FROM WIP_Table";
    
    
    
    
                    connection.Open();
                    using ( SqlCommand command = new SqlCommand(query.ToString(), connection) )
                    {
                        command.Notification = null;
                        DataTable dt = new DataTable();
                        SqlDependency dependency = new SqlDependency(command);
    
                    
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                        
                            if ( connection.State == ConnectionState.Closed )
                                connection.Open();
                            var reader = command.ExecuteReader();
                            dt.Load(reader);
                        
                        if ( dt.Rows.Count > 0 )
                        {
                           
                         
                           TML1 = Int32.Parse(dt.Rows[0]["TML1"].ToString());
                           TML2 = Int32.Parse(dt.Rows[0]["TML2"].ToString());
                           TMT1 = Int32.Parse(dt.Rows[0]["TMT1"].ToString());
                           TMT2 = Int32.Parse(dt.Rows[0]["TMT2"].ToString());
                           MLT1 = Int32.Parse(dt.Rows[0]["MLT1"].ToString());
                           MLT2 = Int32.Parse(dt.Rows[0]["MLT2"].ToString());
                           MLT3 = Int32.Parse(dt.Rows[0]["MLT3"].ToString());
                           MLT4 = Int32.Parse(dt.Rows[0]["MLT4"].ToString());
                           MLT5 = Int32.Parse(dt.Rows[0]["MLT5"].ToString());
                           SNL1 = Int32.Parse(dt.Rows[0]["SNL1"].ToString());
                           SNL2 = Int32.Parse(dt.Rows[0]["SNL2"].ToString());
                           SNL3 = Int32.Parse(dt.Rows[0]["SNL3"].ToString());
                           SNL4 = Int32.Parse(dt.Rows[0]["SNL4"].ToString());
                           SNL5 = Int32.Parse(dt.Rows[0]["SNL5"].ToString());
                           CAV1 = Int32.Parse(dt.Rows[0]["CAV1"].ToString());
                           CAV2 = Int32.Parse(dt.Rows[0]["CAV2"].ToString());
                           CAV3 = Int32.Parse(dt.Rows[0]["CAV3"].ToString());
                           CAV4 = Int32.Parse(dt.Rows[0]["CAV4"].ToString());
                           CAV5 = Int32.Parse(dt.Rows[0]["CAV5"].ToString());
                           IBL1 = Int32.Parse(dt.Rows[0]["IBL1"].ToString());
                           IBL2 = Int32.Parse(dt.Rows[0]["IBL2"].ToString());
                           IBL3 = Int32.Parse(dt.Rows[0]["IBL3"].ToString());
                           IBL4 = Int32.Parse(dt.Rows[0]["IBL4"].ToString());
                           IBL5 = Int32.Parse(dt.Rows[0]["IBL5"].ToString());
                           PAW1 = Int32.Parse(dt.Rows[0]["PAW1"].ToString());
                           PAW2 = Int32.Parse(dt.Rows[0]["PAW2"].ToString());
                           PAW3 = Int32.Parse(dt.Rows[0]["PAW3"].ToString());
                           PAW4 = Int32.Parse(dt.Rows[0]["PAW4"].ToString());
                           PAW5 = Int32.Parse(dt.Rows[0]["PAW5"].ToString());
                           PLB1 = Int32.Parse(dt.Rows[0]["PLB1"].ToString());
                           PLB2 = Int32.Parse(dt.Rows[0]["PLB2"].ToString());
                           PLB3 = Int32.Parse(dt.Rows[0]["PLB3"].ToString());
                           PLB4 = Int32.Parse(dt.Rows[0]["PLB4"].ToString());
                           PLB5 = Int32.Parse(dt.Rows[0]["PLB5"].ToString());
    StringBuilder sb = new StringBuilder();
    sb.Append("TML1,TML2,TMT1,TMT2,MLT1,MLT2,MLT3,MLT4,MLT5,");
    sb.Append("SNL1,SNL2,SNL3,SNL4,SNL5,CAV1,CAV2,CAV3,CAV4,CAV5,");
    sb.Append("IBL1,IBL2,IBL3,IBL4,IBL5,PAW1,PAW2,PAW3,PAW4,PAW5");
    sb.Append("PLB1,PLB2,PLB3,PLB4,PLB5");

    }
     }
                }
                
           
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
                            context.Clients.All.RecieveNotification(MLT3);
               
       }
            
            
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
                    {
                        if (e.Type == SqlNotificationType.Change)
                        {
                            NotificationHub nHub = new NotificationHub();
                            nHub.SendNotifications();
                        }
                    }
    
        }

                   

    :

    Wednesday, October 7, 2015 2:04 PM

Answers

  • User61956409 posted

    Hi RDowdall,

    Please make sure if you have added required script references.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 10:14 PM

All replies

  • User61956409 posted

    Hi RDowdall,

    Please make sure if you have added required script references.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 10:14 PM
  • User64294366 posted

    Hello Fei - Thank-you for your response. I have the script reference as shown below. The failure point is at: notifications.client.receiveNotification = function(MLT3).

    an alert() directly before this line of code fires. An alert inside the function(MLT3) does not.

    Thursday, October 8, 2015 7:44 AM
  • User64294366 posted

    Hello   - I have gotten past the problem described initially by recoding the application.

    I think I may have had a typo somewhere that I did not see.

    In the new version I started by passing one variable to the client side. It worked so I expanded my query and loaded the entire result set of 34 values into an array, which I then passed to the client side. When the page initially loads all values are displayed on the screen.

    However, when I go to the database table and modify the record, the values do not change unless I refresh the html page . The Service Broker is enabled, however it does not seem to be connecting to the application.

    In the global.asax Application_Start I have SqlDependency.Start(strConn). Have any ideas on what might be happening? 

    Wednesday, October 14, 2015 12:35 PM