后台订阅前台无刷新显示
后台
//创建客户端实例
MqttClient client = new MqttClient(IPAddress.Parse(mqttcon.addressip));
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Session["UserInfo"].ToString();
client.Connect(clientId, mqttcon.mqtname, mqttcon.mqtpsd);
client.Subscribe(new string[] { sname }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
void client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
{
//处理接收到的消息
try
{
msg = System.Text.Encoding.Default.GetString(e.Message);
}
catch { }
}
//定时器更新字符串
protected void Timer_number_Tick(object sender, EventArgs e)
{
if (mqttcon.IsSocketRun)
{ this.tx_number.Text = msg; }
}
前台
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="tx_number" runat="server" Text=""></asp:Label>
<asp:Timer ID="Timer_number" runat="server" Interval="1000" OnTick="Timer_number_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
JS
<script type ="text/javascript">
setInterval("loaddata()", 5000);
</script>