简体   繁体   English

SignalR 2仅在页面刷新时更新

[英]SignalR 2 updates only on page refresh

I've developed an SR 2 application using an online example. 我已经使用在线示例开发了SR 2应用程序。 It's not an MVC version and I coded in C#. 它不是MVC版本,我使用C#编写代码。

I query against a database with one table that contains one row of data with many fields. 我使用一个表查询数据库,该表包含一行数据和许多字段。

My problem is that the data displays on initial page load but not after data changes in database. 我的问题是,数据在初始页面加载时显示,但在数据库中的数据更改后却不显示。 If I refresh the page manually, the data updates on the page, reflecting the changes in the database. 如果我手动刷新页面,则页面上的数据将更新,以反映数据库中的更改。 Any clues? 有什么线索吗? If you need more information let me know. 如果您需要更多信息,请告诉我。

Thanks, script on default.aspx page. 谢谢,脚本位于default.aspx页面上。 iData is an array containing values to be displayed. iData是一个包含要显示的值的数组。

$(function () {
        // Declare a proxy to reference the hub.
        var wip = $.connection.wipHub;

        // Create a function that the hub can call to broadcast messages.
        wip.client.receiveWip = function (iData) {
            arr = iData.split(',');
}
});

Hub code here: 集线器代码在这里:

[HubName("wipHub")]
public class WipHub : Hub
{
    string param = "";
    int TML1 = 0; int TML2 = 0; int TMT1 = 0; int TMT2 = 0;
    int MLT1 = 0; int SNL1 = 0; int CAV1 = 0; int IBL1 = 0; int PAW1 = 0; int PLB1 = 0;
    int MLT2 = 0; int SNL2 = 0; int CAV2 = 0; int IBL2 = 0; int PAW2 = 0; int PLB2 = 0;
    int MLT3 = 0; int SNL3 = 0; int CAV3 = 0; int IBL3 = 0; int PAW3 = 0; int PLB3 = 0;
    int MLT4 = 0; int SNL4 = 0; int CAV4 = 0; int IBL4 = 0; int PAW4 = 0; int PLB4 = 0;
    int MLT5 = 0; int SNL5 = 0; int CAV5 = 0; int IBL5 = 0; int PAW5 = 0; int PLB5 = 0;

    [HubMethodName("sendWip")]
    public void SendWip()
    {
        using ( var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString) )
        {
            string query = "Select 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,TML1,TML2,TMT1,TMT2 FROM WIP_Table Where ID=" + 1;
            connection.Open();

            using ( SqlCommand command = new SqlCommand(query, 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();
                    SqlDependency.Start(connection.ConnectionString);
                }

                var reader = command.ExecuteReader();
                dt.Load(reader);
                if ( dt.Rows.Count > 0 )
                {
                    MLT1 = int.Parse(dt.Rows[0]["MLT1"].ToString());
                    MLT2 = int.Parse(dt.Rows[0]["MLT2"].ToString());
                    MLT3 = int.Parse(dt.Rows[0]["MLT3"].ToString());
                    MLT4 = int.Parse(dt.Rows[0]["MLT4"].ToString());
                    MLT5 = int.Parse(dt.Rows[0]["MLT5"].ToString());
                    SNL1 = int.Parse(dt.Rows[0]["SNL1"].ToString());
                    SNL2 = int.Parse(dt.Rows[0]["SNL2"].ToString());
                    SNL3 = int.Parse(dt.Rows[0]["SNL3"].ToString());
                    SNL4 = int.Parse(dt.Rows[0]["SNL4"].ToString());
                    SNL5 = int.Parse(dt.Rows[0]["SNL5"].ToString());
                    CAV1 = int.Parse(dt.Rows[0]["CAV1"].ToString());
                    CAV2 = int.Parse(dt.Rows[0]["CAV2"].ToString());
                    CAV3 = int.Parse(dt.Rows[0]["CAV3"].ToString());
                    CAV4 = int.Parse(dt.Rows[0]["CAV4"].ToString());
                    CAV5 = int.Parse(dt.Rows[0]["CAV5"].ToString());
                    IBL1 = int.Parse(dt.Rows[0]["IBL1"].ToString());
                    IBL2 = int.Parse(dt.Rows[0]["IBL2"].ToString());
                    IBL3 = int.Parse(dt.Rows[0]["IBL3"].ToString());
                    IBL4 = int.Parse(dt.Rows[0]["IBL4"].ToString());
                    IBL5 = int.Parse(dt.Rows[0]["IBL5"].ToString());
                    PAW1 = int.Parse(dt.Rows[0]["PAW1"].ToString());
                    PAW2 = int.Parse(dt.Rows[0]["PAW2"].ToString());
                    PAW3 = int.Parse(dt.Rows[0]["PAW3"].ToString());
                    PAW4 = int.Parse(dt.Rows[0]["PAW4"].ToString());
                    PAW5 = int.Parse(dt.Rows[0]["PAW5"].ToString());
                    PLB1 = int.Parse(dt.Rows[0]["PLB1"].ToString());
                    PLB2 = int.Parse(dt.Rows[0]["PLB2"].ToString());
                    PLB3 = int.Parse(dt.Rows[0]["PLB3"].ToString());
                    PLB4 = int.Parse(dt.Rows[0]["PLB4"].ToString());
                    PLB5 = int.Parse(dt.Rows[0]["PLB5"].ToString());
                    TML1 = int.Parse(dt.Rows[0]["TML1"].ToString());
                    TML2 = int.Parse(dt.Rows[0]["TML2"].ToString());
                    TMT1 = int.Parse(dt.Rows[0]["TMT1"].ToString());
                    TMT2 = int.Parse(dt.Rows[0]["TMT2"].ToString());

                    param = 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
                                +","
                                + TML1
                                + ","
                                + TML2
                                + ","
                                + TMT1
                                + ","
                                + TMT2;

                }

            }

        }

        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<WipHub>();
        context.Clients.All.ReceiveWip(param);

    }
    private void dependency_OnChange( object sender, SqlNotificationEventArgs e )
    {
        if ( e.Type == SqlNotificationType.Change )
        {
            WipHub thisHub = new WipHub();
            thisHub.SendWip();
        }
    }
}

You have to read: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server 您必须阅读: http : //www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server

"You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server." “您无需实例化Hub类或从服务器上自己的代码调用其方法;所有这些操作都是由SignalR Hubs管道为您完成的。SignalR每次需要处理Hub时都会为您的Hub类创建一个新实例。客户端连接,断开连接或对服务器进行方法调用等操作。”

I would recommend that you make the follwing tutorial: http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr 我建议您制作以下教程: http : //www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

After that you should know how to solve your problem. 之后,您应该知道如何解决您的问题。

In hub class you need to write some iterative code so that you can call context.Clients.All.ReceiveWip(param) repeatedly. 在中心类中,您需要编写一些迭代代码,以便可以重复调用context.Clients.All.ReceiveWip(param)。 You can use System.Timers.Timer as: 您可以将System.Timers.Timer用作:

 public class WipHub : Hub
    {
        public static System.Timers.Timer _Timer;
        public void startWip()
        {
            if (_Timer == null)
            {
                _Timer = new System.Timers.Timer(2000);
                _Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
                _Timer.Enabled = true;
                _Timer.Interval = 1000;
            }
        }
        private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            Clients.All.ReceiveWip(SendWip());
        }


        public string SendWip()
        {
            string param = "";
            int TML1 = 0; int TML2 = 0; int TMT1 = 0; int TMT2 = 0;
            int MLT1 = 0; int SNL1 = 0; int CAV1 = 0; int IBL1 = 0; int PAW1 = 0; int PLB1 = 0;
            int MLT2 = 0; int SNL2 = 0; int CAV2 = 0; int IBL2 = 0; int PAW2 = 0; int PLB2 = 0;
            int MLT3 = 0; int SNL3 = 0; int CAV3 = 0; int IBL3 = 0; int PAW3 = 0; int PLB3 = 0;
            int MLT4 = 0; int SNL4 = 0; int CAV4 = 0; int IBL4 = 0; int PAW4 = 0; int PLB4 = 0;
            int MLT5 = 0; int SNL5 = 0; int CAV5 = 0; int IBL5 = 0; int PAW5 = 0; int PLB5 = 0;

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                string query = "Select 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,TML1,TML2,TMT1,TMT2 FROM WIP_Table Where ID=" + 1;
                connection.Open();

                using (SqlCommand command = new SqlCommand(query, 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();
                        SqlDependency.Start(connection.ConnectionString);
                    }

                    var reader = command.ExecuteReader();
                    dt.Load(reader);
                    if (dt.Rows.Count > 0)
                    {
                        MLT1 = int.Parse(dt.Rows[0]["MLT1"].ToString());
                        MLT2 = int.Parse(dt.Rows[0]["MLT2"].ToString());
                        MLT3 = int.Parse(dt.Rows[0]["MLT3"].ToString());
                        MLT4 = int.Parse(dt.Rows[0]["MLT4"].ToString());
                        MLT5 = int.Parse(dt.Rows[0]["MLT5"].ToString());
                        SNL1 = int.Parse(dt.Rows[0]["SNL1"].ToString());
                        SNL2 = int.Parse(dt.Rows[0]["SNL2"].ToString());
                        SNL3 = int.Parse(dt.Rows[0]["SNL3"].ToString());
                        SNL4 = int.Parse(dt.Rows[0]["SNL4"].ToString());
                        SNL5 = int.Parse(dt.Rows[0]["SNL5"].ToString());
                        CAV1 = int.Parse(dt.Rows[0]["CAV1"].ToString());
                        CAV2 = int.Parse(dt.Rows[0]["CAV2"].ToString());
                        CAV3 = int.Parse(dt.Rows[0]["CAV3"].ToString());
                        CAV4 = int.Parse(dt.Rows[0]["CAV4"].ToString());
                        CAV5 = int.Parse(dt.Rows[0]["CAV5"].ToString());
                        IBL1 = int.Parse(dt.Rows[0]["IBL1"].ToString());
                        IBL2 = int.Parse(dt.Rows[0]["IBL2"].ToString());
                        IBL3 = int.Parse(dt.Rows[0]["IBL3"].ToString());
                        IBL4 = int.Parse(dt.Rows[0]["IBL4"].ToString());
                        IBL5 = int.Parse(dt.Rows[0]["IBL5"].ToString());
                        PAW1 = int.Parse(dt.Rows[0]["PAW1"].ToString());
                        PAW2 = int.Parse(dt.Rows[0]["PAW2"].ToString());
                        PAW3 = int.Parse(dt.Rows[0]["PAW3"].ToString());
                        PAW4 = int.Parse(dt.Rows[0]["PAW4"].ToString());
                        PAW5 = int.Parse(dt.Rows[0]["PAW5"].ToString());
                        PLB1 = int.Parse(dt.Rows[0]["PLB1"].ToString());
                        PLB2 = int.Parse(dt.Rows[0]["PLB2"].ToString());
                        PLB3 = int.Parse(dt.Rows[0]["PLB3"].ToString());
                        PLB4 = int.Parse(dt.Rows[0]["PLB4"].ToString());
                        PLB5 = int.Parse(dt.Rows[0]["PLB5"].ToString());
                        TML1 = int.Parse(dt.Rows[0]["TML1"].ToString());
                        TML2 = int.Parse(dt.Rows[0]["TML2"].ToString());
                        TMT1 = int.Parse(dt.Rows[0]["TMT1"].ToString());
                        TMT2 = int.Parse(dt.Rows[0]["TMT2"].ToString());

                        param = 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
                                    + ","
                                    + TML1
                                    + ","
                                    + TML2
                                    + ","
                                    + TMT1
                                    + ","
                                    + TMT2;

                    }

                }

            }
            return param;
        }
    }

And on client side as: 而在客户端上为:

$(function () {
        // Declare a proxy to reference the hub.
        var wip = $.connection.wipHub;

        // Create a function that the hub can call to broadcast messages.
        wip.client.receiveWip = function (iData) {
            arr = iData.split(',');
}
$.connection.hub.start(function() {
wip.server.startWip();
});
});

You can follow the link http://www.niceonecode.com/QA/DotNet/CSharp/SignalR-web-client-(Javascript-Client)-without-ASP_NET/20203 您可以点击链接http://www.niceonecode.com/QA/DotNet/CSharp/SignalR-web-client-(Javascript-Client)-without-ASP_NET/20203

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM