'SignalR multiple chat rooms with Angular and .Net
Good I have a question when I send to all users let me go well, but when I limit it to only users who are in certain groups by passing the Id of the room directly I do not get anything directly from the Hub, the truth is that I do not know what can be I'm doing wrong.
BackEnd
[HttpPost]
public IActionResult SendMessage([FromBody] MessagesForCreationDto message)
{
if (message == null)
{
_logger.LogError("Message object sent from client is null.");
return BadRequest("Message object is null");
}
message.Room = Convert.ToString(message.FK_ChatRoomID);
var messages = _mapper.Map<Messages>(message);
_repository.Messages.Create(messages);
_repository.Messages.SaveAsync();
string not = Newtonsoft.Json.JsonConvert.SerializeObject(message);
//_hubContext.Clients.Group(message.Room).SendAsync("ReceiveMessage", not);
_hubContext.Clients.All.SendAsync("ReceiveMessage", not);
return Ok();
}
FrontEnd
Service
export class ChatService extends DataService {
public hubConnection: HubConnection;
messageReceived: EventEmitter<Messages> = new EventEmitter();
constructor(http: HttpClient) {
super(http, '/chatrooms');
console.log('Inicio el servicio signal con cambio');
const builder = new HubConnectionBuilder();
this.hubConnection = builder.withUrl('https://localhost:5001/chat').build();
this.hubConnection.on('ReceiveMessage', (mensaje) => {
const not: Messages = JSON.parse(mensaje);
this.messageReceived.emit(not);
console.log(mensaje);
});
this.hubConnection.start();
}
}
Ts
msj: Messages[] = [];
getById() {
this.handler = this.chatService.getById(this.id).subscribe(data => {
this.chat = data;
this.msj.push(...this.chat.messages);
console.log(data);
}, error => {
console.log(error);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
