asp.net - SignalR 导致服务器上出现错误请求 400

标签 asp.net asp.net-mvc signalr signalr-hub signalr.client

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

New Relic Data

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

这是SignalR后端代码(我们使用sql server作为signalr背板):

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 并检查问题是否仍然存在。

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

关于asp.net - SignalR 导致服务器上出现错误请求 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36361543/

相关文章:

asp.net-mvc - 我应该将共享的 HTML 生成代码放在 ASP.NET MVC 项目中的什么位置?

c# - 如何从第一项之外的 foreach 枚举

c# - Signalr:当用户为特定用户发送消息时无法发回消息

c# - SignalR 客户端每两秒重新连接到集线器

asp.net - 构建基于 Internet 的 p2p 文本、音频、视频聊天应用程序

asp.net - 如何串联RDLC表达式中的值?

javascript - 如何在表格中显示隐藏特定的div?

signalr - 通过 Web 代理使用 SignalR 客户端

asp.net - ASP.net AJAX 中未捕获 JQuery ajaxStart 事件

c# - 无法在cs文件中打开sql连接 - asp.net