在服务器上看到导致错误请求 400 的 SignalR

SignalR causing bad request 400 seen on the server

我们遇到了 signalR 问题。我们有一个在 signalr 上运行的拍卖网站,用于实时竞价。我们修复了浏览器的一些问题,一切似乎都运行良好。然后我们在我们的服务器上安装了新的 relic,并注意到我们每分钟都会在 signalr 连接、重新连接和中止上收到 http 错误代码 400。这是屏幕截图:

根据 new relic,SignalR 连接和重新连接是网站最耗时的操作。

这是 SignalR 后端代码(我们使用 sql 服务器作为信号器背板):

public class SignalRHub : Hub
{
    public void BroadCastMessage(String msg)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<SignalRHub>();

        hubContext.Clients.All.receiveMessage(msg);
    }
}

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        string appString=string.Empty;

        //Gets the connection string.
        if (System.Configuration.ConfigurationSettings.AppSettings["SignaRScaleoutConn"] != null)
        { 
            appString = System.Configuration.ConfigurationSettings.AppSettings["SignaRScaleoutConn"].ToString();
        }

        GlobalHost.DependencyResolver.UseSqlServer(appString);
        GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromMinutes(15); //I added this timeout, but it is not required.
        app.MapSignalR();
    }
}

javascript客户端看起来是这样的,很长,但大部分都是jQuery影响DOM,我把它都包含进来以防里面有问题.

 $(function () {

            var chatProxy = $.connection.signalRHub;

            $.connection.hub.start();

            chatProxy.client.receiveMessage = function (msg) {

                var all = $(".soon").map(function () {

                    var hiddenModelId = $("#hiddenListingId");

                    if (msg == hiddenModelId.val()) {

                        $.ajax({
                            async: "true",
                            url: "/Listing/AuctionRemainingTime",
                            type: "POST",
                            dataType: 'json',
                            data: '{ "listingID": "' + msg + '"}',
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                if (data != null) {

                                    SoonSettings.HasReloadedThisTick = false;

                                    var element = document.getElementById(msg);

                                    var obj = JSON.parse(data)

                                    // For Clock Counter End Date Time Interval 
                                    // Adds 2 minutes to the soon clock when bid is close to finishing.
                                    var hdID = "hdn" + obj.ListingId;
                                    var hdValue = $("#" + hdID);
                                    if (obj.EndDate != hdValue.val()) {

                                        SoonSettings.HasUpdated = false; //Allows clock to change color once it gets under two minutes.

                                        $('#' + hdID).val(obj.EndDate);
                                        Soon.destroy(element);
                                        Soon.create(element, { //Recreates clock with the before 2 minute tick event.
                                            'due': 'in ' + obj.Seconds + ' seconds',
                                            'layout':'group label-uppercase',
                                            'visual':'ring cap-round progressgradient-00fff6_075fff ring-width-custom gap-0',
                                            'face':'text',
                                            'eventTick': 'tick'
                                        });
                                    }

                                    var highbid = obj.HighBidderURL;

                                    // For Date Ends Info.
                                    var ListingEndDate = $("#tdAuctionListingEndDate");

                                    if (obj.EndDate != ListingEndDate.val()) {
                                        $('#' + hdID).val(obj.EndDate);
                                        ListingEndDate.text(obj.EndDate + " Eastern");
                                        ListingEndDate.effect("pulsate", { times: 5 }, 5000);
                                    }
                                    else
                                    {
                                        $(".Bidding_Current_Price").stop(true, true); ///Removes the pulsating effect.
                                        $(".Bidding_Current_Price").removeAttr("style"); //Removes unnecessary attribute from HTML.
                                    }

                                    //Bid div notification.
                                    if (obj.AcceptedActionCount.replace(/[^:]+:*/, "") > 0) {

                                        if (obj.Disposition != '' && obj.Disposition != null) {
                                            if (obj.Disposition == "Neutral") {
                                                $("#spanNeutralBid").show();
                                                $("#divOutbidNotification").hide();
                                                $("#spanPositiveBid").hide();
                                                $("#divProxyBidNotification").hide();
                                            }
                                            else if (obj.Disposition == "Positive") {
                                                $("#spanPositiveBid").show();
                                                $("#divOutbidNotification").hide();
                                                $("#spanNeutralBid").hide();
                                                $("#divProxyBidNotification").hide();
                                            }
                                            else if (obj.Disposition == "Negative") {
                                                $("#divOutbidNotification").show();
                                                $("#spanNeutralBid").hide();
                                                $("#spanPositiveBid").hide();
                                                $("#divProxyBidNotification").hide();
                                            }
                                            else {
                                                $("#divOutbidNotification").hide();
                                                $("#spanNeutralBid").hide();
                                                $("#divProxyBidNotification").hide();
                                                $("#spanPositiveBid").hide();     
                                            }

                                        }
                                    }

                                    // For Highlight Current Price when it is Updated
                                    var hdCurrentPrice = $("#hdnCurrentPrice");

                                    if (obj.CurrentPrice != hdCurrentPrice.val()) {

                                        $(".Bidding_Current_Price").text(obj.CurrentPrice);
                                        $(".Bidding_Current_Price").effect("pulsate", { times: 5 }, 5000);
                                        $("#hdnCurrentPrice").val(obj.CurrentPrice);
                                    }
                                    else {
                                        $(".Bidding_Current_Price").stop(true, true);
                                        $(".Bidding_Current_Price").removeAttr("style");
                                    }

                                    // For ReservePrice Status
                                    $("#spanReservePriceStatus").html(obj.ReservePriceStatus);
                                    $("#smallReservePriceStatus").html(obj.ReservePriceStatus);

                                    // For Bid Count

                                    var spanBidCounter = $("#spanBidCount");

                                    $(spanBidCounter).text(obj.AcceptedActionCount);


                                    var stringAppend = "<tr id='trhHighBidder'><td><strong>HighBidder</strong></td>";
                                    stringAppend += "<td>";
                                    if (obj.isAdmin == true) {
                                        stringAppend += "<a id='anchorHighBid' href=" + obj.HighBidderURL + ">";
                                        stringAppend += "<span id='spanHighBidder'>" + obj.CurrentListingActionUserName + "</span>"
                                        stringAppend += "</a>";
                                    }
                                    else {
                                        stringAppend += "<span id='spanHighBidderAnonymous'>" + obj.CurrentListingActionUserName + "</span>";
                                    }
                                    stringAppend += "</td></tr>";

                                    if (obj.AcceptedActionCount.replace(/[^:]+:*/, "") > 0) {
                                        if ($("#tblAuctionDetail").find("#rowHighBidder").length > 0) {

                                            if ($("#tblAuctionDetail").find("#trhHighBidder").length > 0) {
                                                $("#trhHighBidder").remove();
                                            }
                                        }
                                        else {

                                            //add tr to table
                                            if (!$("#tblAuctionDetail").find("#trhHighBidder").length > 0) {
                                                $('#tblAuctionDetail > tbody > tr:eq(6)').after(stringAppend);
                                            }
                                        }
                                    }

                                    // For High Bidder

                                    if (obj.isAdmin) {

                                        var anchorElement = $("#anchorHighBid");
                                        $(anchorElement).attr("href", obj.HighBidderURL);

                                        var spanHighBidder = $("#spanHighBidder");
                                        $(spanHighBidder).text(obj.CurrentListingActionUserName);
                                    }
                                    else {
                                        var spanAdminHighBid = $("#spanHighBidderAnonymous");
                                        $(spanAdminHighBid).text(obj.CurrentListingActionUserName)
                                    }

                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {

                            }
                        });
                    }
                });
            };

        });

客户端或服务器信号器代码是否有任何问题需要更改以避免这些错误经常发生? 400 代码几乎每分钟都有出现的趋势。我是 signalR 的新手,对如何使用它编写有效的代码知之甚少。

站点中的实时竞价确实有效,只是想办法避免这些不断出现的错误。感谢任何解释 signalR 工作原理的帮助。

谢谢,

我会尝试改变 SignalR 的传输方式:http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#transport 并检查问题是否仍然存在。

如果可以从 Bad Request 日志中获取 UserAgent,请尝试缩小出现 400 错误的浏览器范围。我想,也许有些浏览器没有使用正确的传输方法连接。