【问题标题】:How to update data using real-time (SignalR)如何使用实时更新数据 (SignalR)
【发布时间】:2014-10-01 13:21:31
【问题描述】:

我写信是为了寻求帮助,关于使用 SignalR 创建实时数据更新。我目前在客户端遇到问题,无法呈现数据内容。

我已经测试了查询命令,它似乎正在返回数据。这让我相信,我的客户端代码可能不正确。

<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript" ></script>
<script src="~/Scripts/jquery.signalR-2.0.1.min.js" type="text/javascript" ></script>
<script src="~/signalr/hubs" type="text/javascript" ></script>
<script type="text/javascript">
$(function () {
    // Declare a proxy to reference the hub.          
    var notifications = $.connection.NotificationHub;
    // Create a function that the hub can call to broadcast messages.
    notifications.client.recieveNotification = function (role, descrip) {
        // Add the message to the page.                    
        $('#spanNewMessages').text(role);
        $('#spanNewCircles').text(descrip);

    };
    // Start the connection.
   $.connection.hub.start().done(function () {
        notifications.server.sendNotifications(function () {
            alert("does it work");
            });
    }).fail(function (e) {
        alert(e);
    });
 </script>
 <h1>New Notifications</h1>
 <div>
<b>New <span id="spanNewMessages"></span> role.</b><br />
<b>New<span id="spanNewCircles"></span> descrip.</b><br />

 </div>

中心类:

  [HubName("NotificationHub")]
  public class notificationHub : Hub
  {
    string role = "";
    string descrip = "";

    [HubMethodName("sendNotifications")]
    public void SendNotifications()
    {
        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dummyConnectionString"].ConnectionString))
        {
            string query = "SELECT [role],[description] FROM [dbo].[User]";
            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();                
                var reader = command.ExecuteReader();
                dt.Load(reader);
                if (dt.Rows.Count > 0)
                {
                    role = dt.Rows[0]["role"].ToString();
                    descrip = dt.Rows[0]["description"].ToString();

                }
            }  
        }

     Clients.All.RecieveNotification(role, descrip);
    }
    private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change)
        {
            notificationHub nHub = new notificationHub();
            nHub.SendNotifications();
        }
    }

}

启动类:

 using Microsoft.Owin;
 using Owin;
 using WebApplication2;

 namespace WebApplication2
 {
   public class Startup
    {
       public void Configuration(IAppBuilder app)
       {
        app.MapSignalR();
       }
    }
  }

任何人都可以提供一些帮助,我可能会在这个任务中出错。谢谢你。

【问题讨论】:

  • 您也应该发布您的 Hub 代码。集线器的SendNotifications 是否被调用?集线器的OnConnected 被调用了吗?
  • @Jonesy,感谢您的回复。我已经用 Hub 类更新了我的代码,以供进一步参考。任何反馈都将受到欢迎。谢谢
  • 当你加载你的页面时,SendNotifications 被调用了吗?
  • @Jonesy,为迟到的回复道歉。抱歉,我对 SignalR 的理解有点陌生,但是如果正在调用 SendNotifications 方法,我该如何测试。非常感谢。

标签: javascript c# jquery asp.net signalr


【解决方案1】:

我模拟了你的应用。你的问题是你从你的中心操作返回一个字符串:

public string SendNotifications()
{
    return context.Clients.All.RecieveNotification(role, descrip);
}

这应该是无效的(你没有返回任何东西,但实际上调用了客户端),并且你也不需要使用 GlobalHost 来获取这里的上下文,只有当上下文不可用时(即调用来自服务器的集线器)。尝试进行以下更改:

[HubMethodName("sendNotifications")]
public void SendNotifications()
{
    //using...

    //IHubContext context = GlobalHost.ConnectionManager.GetHubContext<notificationHub>();
    //return context.Clients.All.RecieveNotification(role, descrip);

    Clients.All.RecieveNotification(role, descrip);
}

Clients.All... 处下断点,看看它是否被触发。如果这些更新解决了您的问题,请告诉我。

【讨论】:

  • 嗨,非常感谢您的帮助和时间。很抱歉通知您,按照您的上述建议,我无法解决此问题。我已经更新了我的代码,如上面的问题所示,您的建议。我无法让断点在Client.All 代码上工作。我尝试在SendNotifications Jquery 中添加一个警告框,但这似乎也不起作用。我的 Jquery 代码在 index.aspx 中,而我的通知代码在单独的 hub 类中,也许我需要将它们绑定在一起?如果可能,请进一步提供建议。
  • 在你的创业课上,你映射了你的信号器吗?如果您浏览到 yoursite/signalr/hubs,如果 SignalR 设置正确,您应该会看到一个 javascript 文件。哎呀,您还需要将此 &lt;script src="~/signalr/hubs" type="text/javascript" &gt;&lt;/script&gt; 更改为 &lt;script src="signalr/hubs" type="text/javascript" &gt;&lt;/script&gt;
  • 亲爱的@Jonesy,感谢您的回复。我再次根据需要进行了更改,但我仍然没有在客户端显示任何内容。我使用http://localhost:4876/Real-Time/Index.aspx/signalr/hubs 对其进行了测试,但我仍然遇到同样的问题。我在 SignalR 的 startUP 类上找不到太多内容。如果我从那里遗漏了一些东西,还有什么我需要添加到该课程中的。非常感谢您的时间和帮助。
  • 对不起,我没能帮上忙。我建议您阅读教程like this one 并感受一下。我们可能忽略了一些小的语法错误。
  • 亲爱的@Jonesy, 非常感谢您的时间和帮助。感谢您的支持和辛勤工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多