首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SignalR 2仅在页面刷新时更新

SignalR 2仅在页面刷新时更新
EN

Stack Overflow用户
提问于 2016-01-06 01:32:33
回答 2查看 771关注 0票数 0

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

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

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

谢谢,default.aspx页面上的脚本。iData是一个包含要显示的值的数组。

代码语言:javascript
复制
$(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代码如下:

代码语言:javascript
复制
[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();
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-01-06 17:24:25

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

“您不必从服务器上自己的代码中实例化集线器类或调用其方法;所有这些都由SignalR集线器管道为您完成。每次需要处理集线器操作时,SignalR都会创建集线器类的新实例。”

我建议你使用下面的教程:http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

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

票数 0
EN

Stack Overflow用户

发布于 2016-01-06 18:09:23

在hub类中,您需要编写一些迭代代码,以便可以重复调用context.Clients.All.ReceiveWip(参数)。您可以将System.Timers.Timer用作:

代码语言:javascript
复制
 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;
        }
    }

在客户端,则为:

代码语言:javascript
复制
$(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-Client)-without-ASP_NET/20203

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34617829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档