gpt4 book ai didi

c# - 无法接收来自群组 Ng-Chat 的消息

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:29 24 4
gpt4 key购买 nike

我已经实现了 ng-chat https://github.com/rpaschoal/ng-chat (信号R)。

我有 3 个用户:User1、User2 和 User3

如果我从 User1 向 User2 发送一条消息,它运行良好).

因此,当我从这个新聊天中发送消息时,用户(User2 和 User3)没有收到任何消息

这是我的 SingalR 中心:

using AdvansysOficina.Api._Core.Infraestructura;
using AdvansysOficina.Api.Generales.Servicios.UsuarioNs;
using Microsoft.AspNetCore.SignalR;
using NgChatSignalR.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AdvansysOficina.Api.Desarrollo.Servicios.ConversacionPuntoNs.HubNs
{
public class ConversacionHub : Hub
{
private static List<ParticipantResponseViewModel> AllConnectedParticipants { get; set; } = new List<ParticipantResponseViewModel>();
private static List<ParticipantResponseViewModel> DisconnectedParticipants { get; set; } = new List<ParticipantResponseViewModel>();
private readonly object ParticipantsConnectionLock = new object();

private ISesion _sesion;
private IUsuarioServicio _usuarioServicio;

public ConversacionHub(ISesion sesion, IUsuarioServicio usuarioServicio)
{
_sesion = sesion;
_usuarioServicio = usuarioServicio;
}

public static IEnumerable<ParticipantResponseViewModel> ConnectedParticipants(string currentUserId)
{
return AllConnectedParticipants
.Where(x => x.Participant.Id != currentUserId);
}

public void Join(string userName, dynamic grupo)
{
lock (ParticipantsConnectionLock)
{
AllConnectedParticipants.Add(new ParticipantResponseViewModel()
{
Metadata = new ParticipantMetadataViewModel()
{
TotalUnreadMessages = 0
},
Participant = new ChatParticipantViewModel()
{
DisplayName = userName,
Id = Context.ConnectionId,
}
});


// This will be used as the user's unique ID to be used on ng-chat as the connected user.
// You should most likely use another ID on your application
//Clients.Caller.SendAsync("generatedUserId", Context.ConnectionId);

Clients.Caller.SendAsync("generatedUserId", Context.ConnectionId);

Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
}
}

public void SendMessage(MessageViewModel message)
{

var sender = AllConnectedParticipants.Find(x => x.Participant.Id == message.FromId);

if (sender != null)
{
Clients.Client(message.ToId).SendAsync("messageReceived", sender.Participant, message);
}
}

public override Task OnDisconnectedAsync(Exception exception)
{
lock (ParticipantsConnectionLock)
{
var connectionIndex = AllConnectedParticipants.FindIndex(x => x.Participant.Id == Context.ConnectionId);

if (connectionIndex >= 0)
{
var participant = AllConnectedParticipants.ElementAt(connectionIndex);

AllConnectedParticipants.Remove(participant);
DisconnectedParticipants.Add(participant);

Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
}

return base.OnDisconnectedAsync(exception);
}
}

public override Task OnConnectedAsync()
{
lock (ParticipantsConnectionLock)
{
var connectionIndex = DisconnectedParticipants.FindIndex(x => x.Participant.Id == Context.ConnectionId);

if (connectionIndex >= 0)
{
var participant = DisconnectedParticipants.ElementAt(connectionIndex);

DisconnectedParticipants.Remove(participant);
AllConnectedParticipants.Add(participant);

Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
}

return base.OnConnectedAsync();
}
}
}
}

我的 signalR 适配器( Angular )

import { ChatAdapter, Message, ParticipantResponse, Group, IChatController } from 'ng-chat';
import { map, catchError } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

import * as signalR from '@aspnet/signalr';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { AlertasHelper } from '../../../shared/helpers/alertas.helper';
import { PushNotificationHelper } from './notifications/push-notification';

export class SignalRAdapter extends ChatAdapter {
public static serverBaseUrl = 'http://192.168.16.51:5021/'; // if running locally
public userId: string;
private grrupo;
private hubConnection: signalR.HubConnection;


constructor(private username: string, private http: HttpClient, private notification: PushNotificationHelper
) {
super();

this.initializeConnection();
}

private initializeConnection(): void {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(`${SignalRAdapter.serverBaseUrl}chat`, { transport: signalR.HttpTransportType.LongPolling })
.build();

this.hubConnection
.start()
.then(() => {
this.joinRoom();

this.initializeListeners();
})
.catch(err => console.log(`Error while starting SignalR connection: ${err}`));
}

private initializeListeners(): void {
this.hubConnection.on('generatedUserId', (userId) => {
// With the userId set the chat will be rendered
this.userId = userId;
});

this.hubConnection.on('messageReceived', (participant, message) => {
// Handle the received message to ng-chat
console.log(message);
this.notification.notify('Nuevo mensaje de: ' + participant.displayName, message);
this.onMessageReceived(participant, message);
});

this.hubConnection.on('friendsListChanged', (participantsResponse: Array<ParticipantResponse>) => {
// Handle the received response to ng-chat
this.onFriendsListChanged(participantsResponse.filter(x => x.participant.id !== this.userId));
});
}

joinRoom(): void {
if (this.hubConnection && this.hubConnection.state === signalR.HubConnectionState.Connected) {
this.hubConnection.send('join', this.username, '');
}
}

listFriends(): Observable<ParticipantResponse[]> {
// List connected users to show in the friends list
// Sending the userId from the request body as this is just a demo
// return this.http
// .post(`${SignalRAdapter.serverBaseUrl}listFriends`, { currentUserId: this.userId })
// .pipe(
// map((res: any) => res),
// catchError((error: any) => Observable.throw(error.error || 'Server error'))
// );
return of([]);

}

getMessageHistory(destinataryId: any): Observable<Message[]> {
// This could be an API call to your web application that would go to the database
// and retrieve a N amount of history messages between the users.
return of([]);
}

sendMessage(message: Message): void {
if (this.hubConnection && this.hubConnection.state === signalR.HubConnectionState.Connected) {
console.log(message);
this.hubConnection.send('sendMessage', message);
}
}

groupCreated(group: Group): void {
console.log( group);
}
}

组件的使用

<ng-chat #chat *ngIf="signalRAdapter && signalRAdapter.userId"
[adapter]="signalRAdapter"
[userId]="signalRAdapter.userId"
[groupAdapter]="signalRAdapter"
(onParticipantChatOpened)="chatOpened($event)"
[historyEnabled]="false">
</ng-chat>

我下载了github的creator page的例子,但是他没有signalr using groups的例子,希望你能帮帮我。

最佳答案

ng-chat 将群组视为个人参与者。调用此事件时,您将必须加入您的房间:

groupCreated(组:组):void {
控制台日志(组);
//调用您的 SignalR 集线器并发送新创建组的详细信息
}

每次创建群组时,ng-chat 都会生成唯一的 ID,因此无论何时从正在运行的 ng-chat 实例创建群组,您都可以跟踪哪个群组。如何处理这些组的持久性取决于您的应用程序。

您可能希望从您的 SignalR 适配器向相关用户推送通知,告知他们的好友列表已更改(他们将能够在此阶段看到该组)。您也可以决定不这样做,仅在创建组的用户发送初始消息时才推送通知(再次,取决于您的应用程序要求和需要)。

您可能还想在适配器上实现 IChatGroupAdapter 以使契约更加明确。

希望这对您有所帮助!

关于c# - 无法接收来自群组 Ng-Chat 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54894884/

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