SignalR2仅在页面刷新时更新

本文关键字:刷新 更新 SignalR2 | 更新日期: 2023-09-27 18:29:07

我使用一个在线示例开发了一个SR2应用程序。它不是MVC版本,我用C#编写。

我对一个数据库进行查询,该数据库有一个表,其中包含一行带有多个字段的数据。

我的问题是,数据在初始页面加载时显示,但在数据库中的数据更改后不显示。如果手动刷新页面,数据会在页面上更新,反映数据库中的更改。有线索吗?如果你需要更多信息,请告诉我。

谢谢,default.aspx页上的脚本。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(',');
}
});

此处的集线器代码:

[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();
        }
    }
}

SignalR2仅在页面刷新时更新

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

"您不会在服务器上实例化Hub类或从自己的代码中调用其方法;所有这些都是由SignalR-Hubs管道为您完成的。SignalR每次需要处理Hub操作时(如客户端连接、断开连接或对服务器进行方法调用时)都会创建Hub类的新实例。"

我建议您制作以下教程:http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

之后你应该知道如何解决你的问题。

在hub类中,您需要编写一些迭代代码,以便调用上下文。Clients.All.ReceiveWip(param)重复。您可以将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;
        }
    }

在客户端为:

$(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();
});
});

你可以关注链接http://www.niceonecode.com/Q-A/DotNet/CSharp/SignalR-web-client-(Javascript客户端)-不带ASP_NET/20203