从服务器错误收到的 Signalr 关闭消息
Posted
技术标签:
【中文标题】从服务器错误收到的 Signalr 关闭消息【英文标题】:Signalr Close Message Recieved From Server Error 【发布时间】:2021-12-05 20:24:23 【问题描述】:我已经处理这个错误很长时间了。我只想让信号器听我的 2 方法。当我评论 (this) 方法时,它可以完美运行。 但它不起作用。有人能帮我吗?有时我可以得到这个值,但它会让我得到如下图所示的错误。我研究了一些页面,但实际上找不到任何答案。
Vuejs
const hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Debug)
.withUrl("http://localhost:7002/ChatHub",
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,
)
.build();
hubConnection.start();
this.connectionId = hubConnection.connectionId;
this.connection = hubConnection;
hubConnection.on("UserConnected",(users) => console.log(users));(this)
hubConnection.on(categoryId, (all) =>
all = JSON.parse(all);
this.userMessage =
id: all.Id,
text: all.Text,
userId:all.UserId,
userName:all.UserName,
categoryName: all.CategoryName,
createdOn: all.CreatedOn,
;
this.messages.push(this.userMessage);
console.log(this.userMessages);
);
,
using Microsoft.AspNetCore.SignalR;
using System.Text;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.Http;
using System;
using System.Web;
using System.Net.Http.Headers;
using System.Linq;
using System.IdentityModel.Tokens.Jwt;
using System.IdentityModel.Tokens;
using System.Collections.Generic;
using System.Threading.Tasks;
using Message.Dal.Concrete;
using Message.Dal.Abstract;
using Message.Dal.Model;
using Microsoft.Extensions.Configuration;
namespace Message.Dal.SignalRHub
public class ChatHub : Hub
private readonly IHubContext<ChatHub> _chatHub;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IElasticRepository<OnlineUserModel> _elasticRepository;
private readonly string _indexName;
public ChatHub(IHubContext<ChatHub> chatHub,IElasticRepository<OnlineUserModel> elasticRepository,IConfiguration configuration)
_chatHub = chatHub;
_httpContextAccessor = new HttpContextAccessor();
_elasticRepository = elasticRepository;
_indexName = configuration["elasticsearchserver:User"].ToString();
public async Task SendMessage(Guid categoryId,string message)
await _chatHub.Clients.All.SendAsync(categoryId.ToString(),message);
public override Task OnConnectedAsync()
var token = string.Empty;
var httpContext = _httpContextAccessor.HttpContext.Request.Cookies;
var onlineUserModel = new OnlineUserModel();
onlineUserModel.Id = Context.ConnectionId;
if(!httpContext.Any())
return Task.CompletedTask;
token = httpContext.Where(x=> x.Key == "CodeChatBackend").FirstOrDefault().Value;
if(AuthenticationHeaderValue.TryParse(token,out var headerVal))
var handler = new JwtSecurityTokenHandler();
var val = handler.ReadJwtToken(headerVal.ToString());
onlineUserModel.UserName = val.Claims.FirstOrDefault(x => x.Type == "Name").Value;
var checkOnlineUser = _elasticRepository.GetUserAsync(onlineUserModel.UserName,_indexName);
if(checkOnlineUser.Result == null)
_elasticRepository.CreateUserAsync(onlineUserModel.Id,onlineUserModel,_indexName);
var getOnlineUser = _elasticRepository.GetAllAsync(_indexName);
_chatHub.Clients.All.SendAsync("UserConnected",getOnlineUser);(this)
base.OnConnectedAsync();
return Task.CompletedTask;
public override Task OnDisconnectedAsync(Exception exception)
var httpContext = _httpContextAccessor.HttpContext.Request.Cookies;
var token = string.Empty;
var result = _elasticRepository.DeleteUserAsync(Context.ConnectionId,_indexName);
var getOnlineUser = _elasticRepository.GetAllAsync(_indexName);
_chatHub.Clients.All.SendAsync("UserConnected",getOnlineUser);(this)
base.OnDisconnectedAsync(exception);
return Task.CompletedTask;
当我删除 onconnectedasync 客户端方法时。它完美地工作。我错过了什么吗?
【问题讨论】:
【参考方案1】:我修好了!完全是我的错。我试图制作一个集线器来连接不同的方法。要解决此问题:创建第二个 hub 类并配置您的启动,然后使用新连接调用 hub 方法。
【讨论】:
以上是关于从服务器错误收到的 Signalr 关闭消息的主要内容,如果未能解决你的问题,请参考以下文章