gpt4 book ai didi

c# - 一段时间后信号 R 连接状态为 0

转载 作者:行者123 更新时间:2023-12-03 20:50:50 25 4
gpt4 key购买 nike

我正在使用 @aspnet/signalr要连接到集线器,然后从集线器调用该方法以返回一些数据。
当我第一次初始化服务器时,连接正常建立,但是如果我刷新窗口 3 或 4 次,客户端将停止向服务器发送连接请求。
我尝试记录 HubConnection , connectionState起初等于 1但在问题产生后 connectionState总是等于 0这是我构建 hubConnection 的方式:

buildConnection() {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.tradesService.getStockQuotationsHubUrl())
.build();
this.hubConnection.serverTimeoutInMilliseconds = 1000 * 10;
this.hubConnection.onclose(() => setTimeout(() => this.startSignalRConnection(), 2000));}
这是我启动 hubConnection 的方式:
startConnection() {
this.hubConnection
.start()
.then(() => {
this.hubConnection.on('updateMethod', (data: any) => {

this.store.push([
{ type: 'update', key: data.stockID, data },
]);

});
this.dataSource = new DataSource({
store: this.store,
reshapeOnPush: true,
});
});}
注意:我在我的页面中一次列出了 5 个集线器,但唯一有问题的是这个。
[更新]
问题出在服务器上,因为当我重新启动服务器时,客户端和服务器之间会重新建立连接,但是如果客户端多次刷新或退出页面,集线器甚至不会尝试连接到客户端
public class StockQuotationsHub : Microsoft.AspNetCore.SignalR.Hub
{
private string SectorID { get; set; }
private int TradingSession { get; set; }
private int MarketID { get; set; }

public static StockQuotationExt stockQuotationExt { get; set; }
private readonly StockQuotationTicker _stockTicker;

public StockQuotationsHub(StockQuotationTicker stockTicker){
_stockTicker = stockTicker;
}

public override Task OnConnectedAsync(){
return base.OnConnectedAsync();
}

public IEnumerable<StockQuotation> GetAllStockQuotations(string[] stockID, string sectorID, int tradingSession, int marketType){
return _stockTicker.
GetAllStocks(Context.ConnectionId, stockID, sectorID, tradingSession, marketType);
}

public override async Task OnDisconnectedAsync(Exception exception){
await base.OnDisconnectedAsync(exception);
}
这是我的股票代码类:
public IEnumerable<StockQuotation> GetAllStocks(string connectionId, string[] stockID, string sectorID, int tradingSession, int marketType)
{
_stocks = new List<StockQuotation>();
_stocks = Task.Run(async () => await GetStockQuotationModelAsync("", 0, 1, 0)).Result.ToList();
this.MaxTimeStamp = _stocks.Max(s => s.TStamp);
this.SectorID = sectorID;
this.TradingSession = tradingSession;
this.MarketID = marketType;

AddToGroups(connectionId, stockID);

if (_timer==null)
_timer = new Timer(UpdateStockPrices, null, _updateInterval, _updateInterval);

if (stockID.Length == 0)
{
return _stocks;
}
else
{
var stocksList = new List<StockQuotation>();
foreach (var stock in stockID)
{
stocksList.AddRange(_stocks.Where(s => s.StockID == stock).ToList());
}
return stocksList;
}
}

private void AddToGroups(string connectionId, string[] stockID)
{
if (_stocks.Count > 0)
{
if (stockID.Length == 0)
{
Hub.Groups.AddToGroupAsync(connectionId, "ALL");
}
else
{
foreach (var stock in stockID)
{
Hub.Groups.AddToGroupAsync(connectionId, stock);
var s = _stocks.FirstOrDefault(s => s.StockID == stock);
if(s != null)
{
s.Snapshots = new List<double>(GetStockQuotationSnapshots(stock));
}
}
}
}
}
private void AddToGroups(string connectionId, string[] stockID)
{
if (_stocks.Count > 0)
{
if (stockID.Length == 0)
{
Hub.Groups.AddToGroupAsync(connectionId, "ALL");
}
else
{
foreach (var stock in stockID)
{
Hub.Groups.AddToGroupAsync(connectionId, stock);
var s = _stocks.FirstOrDefault(s => s.StockID == stock);
if(s != null)
{
s.Snapshots = new List<double>(GetStockQuotationSnapshots(stock));
}
}
}
}
}
我非常感谢您的帮助。

最佳答案

最终问题是项目类型是MVC ,所以我把它改成 webApi一切顺利

关于c# - 一段时间后信号 R 连接状态为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62951001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com