This repository has been archived on 2026-05-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
chatless/frontend/src/app/services/chat/chat.service.ts
2021-05-30 22:35:04 +02:00

76 lines
1.4 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { io, Socket } from 'socket.io-client';
export interface ChatInfo {
username: string,
date: Date,
room: string,
message: string
}
@Injectable({
providedIn: 'root'
})
export class ChatService {
private socket: Socket | undefined;
private url: string;
private room: string;
constructor() {
this.url = '';
this.room = '';
}
setUrl(url: string){
this.url = url;
this.setSocket();
}
setRoom(room: string){
this.room = room;
}
setSocket(){
this.socket = io(this.url, {
withCredentials: true
});
}
sendMessage(sender: string | null, receiver: string | null, room: string, message: string) {
if(receiver === null){
// @ts-ignore
this.socket.emit(room, {
username: sender,
date: new Date(),
room: room,
message: message
});
}
else{
// @ts-ignore
this.socket.emit('privateroom', {
sender: sender,
receiver: receiver,
date: new Date(),
message: message
});
}
}
leaveRoom(): void {
// @ts-ignore
this.socket?.disconnect();
}
onNewMessage(room: string): Observable<ChatInfo[]> {
return new Observable(observer => {
// @ts-ignore
this.socket.on(room, (data: ChatInfo[]) => {
//console.log(data);
observer.next(data);
});
});
}
}