From d18735bd4bc381ab53ece1967014f8cf36f7693f Mon Sep 17 00:00:00 2001 From: gokhoal Date: Wed, 10 Jun 2026 14:30:17 +0200 Subject: [PATCH] services chat et discussion --- src/app/core/chat/chat.service.ts | 30 +++++++++++++++++++ src/app/core/chat/discussion.service.ts | 15 ++++++++++ .../messages-send/messages-send.component.ts | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/app/core/chat/chat.service.ts create mode 100644 src/app/core/chat/discussion.service.ts diff --git a/src/app/core/chat/chat.service.ts b/src/app/core/chat/chat.service.ts new file mode 100644 index 0000000..a4dd04d --- /dev/null +++ b/src/app/core/chat/chat.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core'; +import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr'; + +@Injectable({ providedIn: 'root' }) +export class ChatService { + + private hub: HubConnection; + + constructor() { + this.hub = new HubConnectionBuilder() + .withUrl('https://localhost:5001/hubs/chat') + .withAutomaticReconnect() + .build(); + } + + async connect() { + if (this.hub.state === HubConnectionState.Disconnected) { + await this.hub.start(); + } + } + + async sendMessage(discussionId: string, content: string) { + await this.connect(); // s'assure que la connexion est active + await this.hub.invoke('SendMessage', discussionId, content); + } + + onMessage(callback: (message: any) => void) { + this.hub.on('ReceiveMessage', callback); + } +} \ No newline at end of file diff --git a/src/app/core/chat/discussion.service.ts b/src/app/core/chat/discussion.service.ts new file mode 100644 index 0000000..4c14d21 --- /dev/null +++ b/src/app/core/chat/discussion.service.ts @@ -0,0 +1,15 @@ +import {Observable} from "rxjs"; +import {Discussion} from "../../pages/menu/menu-users/menu-users.component"; +import {HttpClient} from "@angular/common/http"; +import {inject, Injectable} from "@angular/core"; + +@Injectable({ providedIn: 'root' }) +export class discussionsService { + + private http = inject(HttpClient); + private apiUrl = 'https://localhost:5001/API'; + + getDiscussions(): Observable { + return this.http.get(`${this.apiUrl}/discussions`); + } +} \ No newline at end of file diff --git a/src/app/pages/messages/messages-send/messages-send.component.ts b/src/app/pages/messages/messages-send/messages-send.component.ts index e4dfefc..9ac756a 100644 --- a/src/app/pages/messages/messages-send/messages-send.component.ts +++ b/src/app/pages/messages/messages-send/messages-send.component.ts @@ -1,6 +1,6 @@ import { Component, inject, Input } from '@angular/core'; import { FormControl, FormsModule, ReactiveFormsModule } from "@angular/forms"; -import { ChatService } from "../../../services/api/chat.service"; +import { ChatService } from "../../../core/chat/chat.service"; @Component({ selector: 'app-messages-send',