信号通知 [英] Signal r notification
问题描述
我正在创建一个使用信号 r 发送通知的应用程序.我正在使用 VS 2012.在我的通知视图中,我在 @model App.Models.Notification
中添加了以下代码.
I am creating an application using signal r to send notification. I am using VS 2012. In my Notification view I have added the code below in @model App.Models.Notification
.
@{
ViewBag.Title = "Index";
}
@section Scripts
{
<script src="/Scripts/jquery-1.8.20.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.js"></script>
<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var proxy = $.connection.notificationHub;
alert(proxy);
$("#button1").click(function () {
alert($("#text1").val());
proxy.server.sendNotifications($("#text1").val());
alert(12);
});
$.connection.hub.start();
alert(14);
});
</script>
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<input id="text1" type="text" />
<input id="button1" type="submit" value="Send" />
}
点击按钮后,sendNotifications()
不会被调用,通知也不会发送给客户端.
On click of the button, sendNotifications()
is not getting called and the notification is not sending to client.
这是枢纽类
public class NotificationHub : Hub
{
public void Hello()
{
Clients.All.hello();
}
public void SendNotifications(string message)
{
Clients.All.receiveNotification(message);
}
}
谁能帮我解决问题
推荐答案
您还需要创建一个 owin 启动类.我会将代码放在这里(与您的视图相同):-
You also need to create a owin startup class. I will put code here for that (view same as your) :-
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var proxy = $.connection.notificationHub;
alert(proxy);
$("#button1").click(function () {
alert($("#text1").val());
proxy.server.sendNotifications($("#text1").val());
alert(12);
});
$.connection.hub.start();
alert(14);
});
</script>
通知中心如:
public class NotificationHub : Hub
{
public void Hello()
{
Clients.All.hello();
}
public void SendNotifications(string message)
{
Clients.All.receiveNotification(message);
}
}
现在最重要的是你需要创建一个 owin 启动类来启动信号 r,代码如下:
Now Most importnant you need to create a owin startup class to start signal r, code like :
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
这篇关于信号通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!