Asp.net
SignalR 導致伺服器上出現錯誤請求 400
我們遇到了 signalR 的問題。我們有一個在 Signalr 上執行的拍賣網站,用於實時出價。我們修復了瀏覽器的一些問題,一切似乎都執行良好。然後我們在我們的伺服器上安裝了新的 relic,並註意到每分鐘我們都會在 signalr 連接、重新連接和中止時收到 http 錯誤程式碼 400。這是一個螢幕截圖:
SignalR 連接和重新連接是站點根據新遺跡最耗時的操作。
這是 SignalR 後端程式碼(我們使用 sql server 作為信號器背板):
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 錯誤。我認為,也許,某些瀏覽器沒有使用正確的傳輸方法連接。
