Add room choice
This commit is contained in:
parent
3eabc8e329
commit
a20364243d
1 changed files with 13 additions and 5 deletions
|
|
@ -15,12 +15,13 @@ export interface ChatInfo {
|
||||||
})
|
})
|
||||||
export class ChatService {
|
export class ChatService {
|
||||||
|
|
||||||
private socket: Socket;
|
private socket: Socket | undefined;
|
||||||
private url: string;
|
private url: string;
|
||||||
|
private room: string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.url = '';
|
this.url = '';
|
||||||
this.socket = io();
|
this.room = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
setUrl(url: string){
|
setUrl(url: string){
|
||||||
|
|
@ -28,6 +29,10 @@ export class ChatService {
|
||||||
this.setSocket();
|
this.setSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setRoom(room: string){
|
||||||
|
this.room = room;
|
||||||
|
}
|
||||||
|
|
||||||
setSocket(){
|
setSocket(){
|
||||||
this.socket = io(this.url, {
|
this.socket = io(this.url, {
|
||||||
withCredentials: true
|
withCredentials: true
|
||||||
|
|
@ -35,6 +40,7 @@ export class ChatService {
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(username: string | null, room: string, message: string) {
|
sendMessage(username: string | null, room: string, message: string) {
|
||||||
|
// @ts-ignore
|
||||||
this.socket.emit(room, {
|
this.socket.emit(room, {
|
||||||
username: username,
|
username: username,
|
||||||
date: new Date(),
|
date: new Date(),
|
||||||
|
|
@ -43,10 +49,12 @@ export class ChatService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewMessage() {
|
onNewMessage(room: string): Observable<ChatInfo[]> {
|
||||||
return new Observable(observer => {
|
return new Observable(observer => {
|
||||||
this.socket.on('general', infos => {
|
// @ts-ignore
|
||||||
observer.next(infos);
|
this.socket.on(room, (data: ChatInfo[]) => {
|
||||||
|
console.log(data);
|
||||||
|
observer.next(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue