我正在使用SignalR
,并且不想使用JavaScript
连接到集线器。
这是我的代码的一部分:
index.aspx:
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<div runat="server" id="DivMessage" style="width:100%; height: 250px;"></div>
<asp:TextBox runat="server" ID="TextBoxName"></asp:TextBox>
<asp:Button runat="server" ID="ButtonLogin" Text="LOGIN" OnClick="ButtonLogin_Click"/>
<asp:Button runat="server" ID="ButtonMessage" Text="SEND" OnClick="ButtonMessage_Click"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
index.aspx.cs:
namespace ASPNetSignalR
{
public partial class index : System.Web.UI.Page
{
private static string uri = "http://localhost:4933";
private static HubConnection connection;
private static IHubProxy proxy;
...
}
protected void ButtonLogin_Click(object sender, EventArgs e)
{
RunSignalRClient();
}
protected void ButtonMessage_Click(object sender, EventArgs e)
{
SendMessage("Ehsan: Hello!");
}
public void RunSignalRClient()
{
connection = new HubConnection(uri);
proxy = connection.CreateHubProxy("ASPNetSignalRHub");
connection.Start().Wait();
proxy.On<string>("SendMessage", SendMessage =>
{
DivMessage.InnerHtml += "<p>" + SendMessage + "</p>";
});
}
public void SendMessage(string message)
{
proxy.Invoke<string>("SendMessage", new object[] { message });
}
}
Startup.cs:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(ASPNetSignalR.Startup))]
namespace ASPNetSignalR
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
运行此命令时,一切正常,DivMessage.InnerHtml += "<p>" + SendMessage + "</p>";
运行(如下图所示),但网页上没有任何显示。
我在哪里错了?
谢谢
答案 0 :(得分:0)
尝试以下方法:
$("#DivMessage").append(SendMessage);