代码之家  ›  专栏  ›  技术社区  ›  Carlos Jimenez Bermudez

SignalR导致服务器上出现错误请求400

  •  14
  • Carlos Jimenez Bermudez  · 技术社区  · 8 年前

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

    New Relic Data

    根据新的遗迹,SignalR连接和重新连接是站点最耗时的操作。

    下面是SignalR后端代码(我们使用sqlserver作为信号器背板):

    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如何工作的帮助都将受到赞赏。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   TNT    8 年前

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

    如果可能从错误请求日志中获取UserAgent,请尝试缩小哪些浏览器出现400错误的范围。我认为,也许,一些浏览器没有连接到正确的传输方法。