我正在使用RabbitMQ来获取实时数据。我建立连接并成功获取数据。我可以在Debug行中打印它,但它不会更新我的Label。
这是我的代码:
private void InitRabbit()
{
factory.UserName = this.user;
factory.Password = this.password;
factory.HostName = this.host;
factory.VirtualHost = "/";
try
{
using (IConnection connection = factory.CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.ExchangeDeclare(realTimeExchange, "topic");
string queueName = channel.QueueDeclare();
StringBuilder sb = new StringBuilder();
sb.Append("QEH").Append(".").Append("QEH").Append(".*");
channel.QueueBind(queueName, realTimeExchange, sb.ToString());
Console.WriteLine("Waiting for messages");
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queueName, true, consumer);
while (true)
{
BasicDeliverEventArgs e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
Console.WriteLine(Encoding.ASCII.GetString(e.Body));
string msg = Encoding.ASCII.GetString(e.Body);
JavaScriptSerializer ser = new JavaScriptSerializer();
Alerts alert = ser.Deserialize<Alerts>(msg);
updatealert(alert); //for getting the Alerts which is my internal class
}
}
rabbitrunning = true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Rabbit not running : " + ex.Message);
}
}
这是我的updatealert函数:
private void updatealert(Alerts alert)
{
alertext.Text = "Count " + alert.CarName;
System.Diagnostics.Debug.WriteLine("publish : " + alert.CarName);//its writting the message correctly but its not updating my Alertext label.
}
以下是更新面板结构:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<legend>UpdatePanel</legend>
<asp:Label ID="alertext" runat="server" Text="change" ></asp:Label>
<asp:Label ID="Label1" runat="server" Text="change" ></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Get Real Data" />
当rabbitMQ更新内容时,如何更改标签文本?
更新:在长途径和错误之后我发现兔子每秒发送大量消息,因此更新面板没有快速更新,所以我尝试将更新面板作业放在其他线程但我有没有成功,是否有任何建议如何更新面板处理非常快的过程?
问候。
答案 0 :(得分:0)
尝试调用UpdatePanel1.Update();设置标签文本后
答案 1 :(得分:0)
RabbitMQ不会每秒发送大量消息,除非您每秒传播大量消息。如果您尝试每秒捕获大量消息,我认为您应该重新考虑您的技术选择。 ASP.Net将作为连接到HTML5文档的桥梁更好地工作,比如使用它来重新传送您在HTML5页面上的websocket上的json文件,因为这样可以为回发提供更好的细粒度控制。