信号r通知

时间:2015-11-26 01:13:02

标签: javascript .net model-view-controller signalr signalr.client

我正在创建一个使用信号r发送通知的应用程序。我正在使用VS 2012.在我的通知视图中,我在load: function (id) { var self = this; KeyWord.byId( id, function (data) { if (data.add_content == 1) { $("#add_content").attr('checked', true); } else { $("#add_content").attr('checked', false); } self.formHandler.load(data); }, function (error) { }, function () { } ); } 中添加了以下代码。

@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()

有人可以帮我解决问题

1 个答案:

答案 0 :(得分:0)

您还需要创建一个owin启动类。我将把代码放在这里(与你的相同): -

<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,代码如下:

    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }