我使用Visual Studio 2010和jquery signal r 1.1.4发送通知,就像我们在facebook中一样。 以下是使用的Hub方法
public class BroadcastMessage:Hub
{
public void BroadcastNotifications(string message)
{
Utility.AddNotification(message); //to save data to database
int UnreadCount = Utility.getUnreadMessageCount(); //get the unread count
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastMessage>();
context.Clients.All.receiveNotification(message, UnreadCount);
}
public void SendMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<BroadcastMessage>();
context.Clients.All.updateMessages();
}
}
部分视图我有文本bx输入通知消息和发送按钮是 @model MyProject.Models.Notification
@
using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Notification</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LogonID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LogonID)
@Html.ValidationMessageFor(model => model.LogonID)
</div>
<p>
<input type="button" id="broadcastNotification" value="Send" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
<script src="~/Scripts/jquery-1.12.0.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
$.connection.hub.logging = true;
var proxy = $.connection.broadcastMessage;
$.connection.hub.start().done(function () {
$('#broadcastNotification').click(function () {
proxy.server.broadcastNotifications($("#Message").val()).done(function () {
proxy.server.sendMessages();
});
});
});
});
</script>
}
Global.asax文件是
protected void Application_Start()
{
//To register the default Hubs route
RouteTable.Routes.MapHubs();
}
这里我的信号r hub方法没有在点击发送按钮时被触发。
我试过.getComponents()
我得到错误“协议错误:未知传输。”有人可以告诉我我在这里犯了什么错误。
答案 0 :(得分:0)
我建议你在你的创业公司中添加一些日志记录:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
ConfigureAuth(app);
app.MapSignalR(hubConfiguration);
}
}