C
C#14mo ago
wwww

✅ Postman / SignalR

hi, so i have hub
public class ChatHub : Hub
{
private readonly IApplicationDbContext _db;
public ChatHub(IApplicationDbContext context)
{
_db = context;
}

public override async Task OnConnectedAsync()
{
await Clients.Caller.SendAsync("loadMessageHistory", _db.Messages.ToList());
await base.OnConnectedAsync();
}
}
public class ChatHub : Hub
{
private readonly IApplicationDbContext _db;
public ChatHub(IApplicationDbContext context)
{
_db = context;
}

public override async Task OnConnectedAsync()
{
await Clients.Caller.SendAsync("loadMessageHistory", _db.Messages.ToList());
await base.OnConnectedAsync();
}
}
is it possible to send data with first connection request? for do like...
int chatId = // Getting id from body
var chat = _db.Chats.Inclued(c => c.Messages).FirstOrDefault(c => c.Id == chatId);
// var messages = _db.Messages.Where(m => m.ChatId == chatId).ToList();
if(messages != null)
{
await Clients.Caller.SendAsync("loadMessageHistory", chat/messages);
await base.OnConnectedAsync();
}
else
{
// Bad connection or smthing like this
}
int chatId = // Getting id from body
var chat = _db.Chats.Inclued(c => c.Messages).FirstOrDefault(c => c.Id == chatId);
// var messages = _db.Messages.Where(m => m.ChatId == chatId).ToList();
if(messages != null)
{
await Clients.Caller.SendAsync("loadMessageHistory", chat/messages);
await base.OnConnectedAsync();
}
else
{
// Bad connection or smthing like this
}
or maybe there is some conventional way to do that
No description
4 Replies
wwww
wwwwOP14mo ago
or that thjing have nothing to do here and i need to create new method where user connects to specific "Chat" (group) by chatId (from body) with default request, not connection one
wwww
wwwwOP14mo ago
public async Task ConnectToChat(int chatId, int userId)
{
await Clients.All.SendAsync($"User {userId} joined chat {chatId}");

var Messages = _db.Messages.Where(m => m.ChatId == chatId);
if (Messages.Any())
await Clients.Caller.SendAsync("ClientJoined", Messages.ToList());
else
await Clients.Caller.SendAsync("ClientJoined", "There is no messages in this chat");
}
public async Task ConnectToChat(int chatId, int userId)
{
await Clients.All.SendAsync($"User {userId} joined chat {chatId}");

var Messages = _db.Messages.Where(m => m.ChatId == chatId);
if (Messages.Any())
await Clients.Caller.SendAsync("ClientJoined", Messages.ToList());
else
await Clients.Caller.SendAsync("ClientJoined", "There is no messages in this chat");
}
got something like this
No description
wwww
wwwwOP14mo ago
so, as i get, first connection to hub, have nothing to do with connecting to group?
artya
artya14mo ago
You can add query parameters to the "first connection request" The other stuff I don't really understand, hard to read rn FeelsDankMan
Want results from more Discord servers?
Add your server