c# – $.connections无法读取未定义的集线器(SignalR)

我将尝试将数据发送到服务器whit SignalR 2.但我收到此错误:

Uncaught TypeError: Cannot read property ‘meldingenHub’ of undefined

这是服务器上的C#代码:

public class MeldingenHub : Hub
{
    public void Meld(string blogitem, string verdiendePunten)
    {
        Clients.All.BroadcastMessage(blogitem, verdiendePunten);
    }
}

这里我包含了所需的所有文件:

<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>

这是客户端代码,它给出了粗体代码的错误.

var medlingenHub = $.connection.meldingenHub;

medlingenHub.client.broadcastMessage = function (blogitem, verdiendePunten) {
    $("#verdiendepunten").html(verdiendePunten);
};

$.connection.hub.start().done(function () {

    var json = {
        ID: parseInt(this.dataset.id),
        Type: this.dataset.type,
        GebruikerID: "@(Model.DeBlog.Gebruiker.Id)",
        Punten: parseInt(this.dataset.punten),
        GestemdeGebruikerID: "@(Model.AangemeldeGebruiker)"
    };

    $.ajax({
        url: "../api/Stem/Toevoegen?apiKey=@Model.ApiKey",
        type: "PUT",
        data: json,
        success: function (returnData) {

            if (returnData.Oke == false) {

                toonError(returnData)
            }
            else {

                plaatsKleuren(returnData);
                medlingenHub.server.meld(data.ID, data.Punten);
            }
        }
    });
});

请注意上面的代码:单击按钮时代码执行.

我还在Startup.Auth.cs文件中添加了这一行:

public void ConfigureAuth(IAppBuilder app)
{
    app.MapSignalR();
    // other code
}

我按照asp.net的教程.任何人都可以帮助我吗?

解决方法:

我找到了.我正在使用$.connection的错误范围.这是我完成的完整代码:

var punten = document.getElementsByClassName("punten-do-ajax");
var aantal = punten.length;

var conn = $.connection;

for (var i = 0; i < aantal; i++) {

    punten[i].addEventListener("click", function () {

        @if (Model.DeBlog.StemmenToegelaten)
        {
            <text>
            votes(conn, this);
            </text>
        }
    });
}

function votes(conn, sender) {
    var medlingenHub = conn.meldingenHub;

    medlingenHub.client.broadcastMessage = function (blogitem, verdiendePunten) {
        $("#verdiendepunten").html(verdiendePunten);
    };

    conn.hub.start().done(function () {

        var json = {
            ID: parseInt(sender.dataset.id),
            Type: sender.dataset.type,
            GebruikerID: "@(Model.DeBlog.Gebruiker.Id)",
            Punten: parseInt(sender.dataset.punten),
            GestemdeGebruikerID: "@(Model.AangemeldeGebruiker)"
        };

        $.ajax({
            url: "../api/Stem/Toevoegen?apiKey=@Model.ApiKey",
            type: "PUT",
            data: json,
            success: function (returnData) {

                if (returnData.Oke == false) {

                    toonError(returnData)
                }
                else {

                    plaatsKleuren(returnData);
                    medlingenHub.server.meld(returnData.ID, returnData.Punten);
                }
            }
        });
    });
}
上一篇:javascript – 尝试使用connection.Start()连接到服务器时出现SignalR Client错误


下一篇:CEF预览