This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsignalr-adapter.ts
80 lines (65 loc) · 2.81 KB
/
signalr-adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ChatAdapter, IChatGroupAdapter, User, Group, Message, ChatParticipantStatus, ParticipantResponse, ParticipantMetadata, ChatParticipantType, IChatParticipant } from 'ng-chat';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import * as signalR from "@aspnet/signalr";
export class SignalRAdapter extends ChatAdapter {
public userId: string;
private hubConnection: signalR.HubConnection
public static serverBaseUrl: string = 'https://ng-chat-api.azurewebsites.net/'; // Set this to 'https://localhost:5001/' if running locally
constructor(private username: string, private http: HttpClient) {
super();
this.initializeConnection();
}
private initializeConnection(): void {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(`${SignalRAdapter.serverBaseUrl}chat`)
.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.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'))
);
}
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)
this.hubConnection.send("sendMessage", message);
}
}