-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommunication.service.ts
126 lines (108 loc) · 4.27 KB
/
communication.service.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {Injectable} from "@angular/core";
import {
ContactInfo,
ContactReference,
DialogInfo,
DialogReference,
Message,
StatusChangedEvent,
UserInfo
} from "../model/model";
import * as SocketIO from "socket.io-client";
import {Subject} from "rxjs/Subject";
import {AuthService} from "./auth/auth.service";
import {ModelService} from "./model.service";
const ScalaJs = require('../scalajs/scala-js-fastopt.js');
// const ScalaJs = require('../scalajs/scala-js-opt.js');
@Injectable()
export class CommunicationService {
private loggedIn: boolean;
public loginSubject: Subject<ContactInfo>;
public selectDialogSubject: Subject<DialogReference>;
public selectAboutSubject: Subject<boolean>;
private contactsStatusChangeSubject: Subject<StatusChangedEvent>;
private newMessagesSubject: Subject<Message>;
private host: string = "http://localhost:3000";
private socket: SocketIOClient.Socket;
private transportService: any | undefined;
constructor(private authService: AuthService, private modelService: ModelService) {
this.socket = SocketIO(this.host);
this.transportService = new ScalaJs.TransportService(this.socket);
this.loggedIn = false;
this.loginSubject = new Subject<ContactInfo>();
this.selectDialogSubject = new Subject<DialogReference>();
this.selectAboutSubject = new Subject<boolean>();
this.contactsStatusChangeSubject = new Subject<StatusChangedEvent>();
this.newMessagesSubject = new Subject<Message>();
}
login(userInfo: UserInfo): void {
if (!this.loggedIn) {
this.loggedIn = true;
this.transportService.addUser(userInfo).then(contactInfo => {
this.modelService.setContactInfo(contactInfo);
this.transportService.sendConnect(contactInfo.contactReference);
this.listen();
this.initModel().then(() => {
this.loginSubject.next(contactInfo);
});
});
}
}
private initModel(): Promise<boolean> {
return this.transportService.listContacts()
.then((contacts: ContactInfo[]) => this.modelService.setContacts(contacts))
.then(() => {
this.transportService.listDialogs(this.modelService.getContactInfo().contactReference)
.then((dialogs: DialogInfo[]) => {
this.modelService.setDialogs(dialogs);
this.modelService.setContactsLastMessages(dialogs);
dialogs.forEach(info => {
this.transportService.listMessages(info.dialogReference)
.then((messages: Message[]) => this.modelService.setMessages(info.dialogReference, messages))
.then(() => {
return true;
});
});
})
});
}
private listen(): void {
this.transportService.listenNewUsers(this.contactsStatusChangeSubject);
this.transportService.listenNewMessages(this.modelService.getContactInfo().contactReference, this.newMessagesSubject);
this.contactsStatusChangeSubject.subscribe(statusChangedEvent => {
if (statusChangedEvent.online) {
this.modelService.addContact(statusChangedEvent.contact);
} else {
this.modelService.removeContact(statusChangedEvent.contact)
}
});
this.newMessagesSubject.subscribe(message => this.modelService.addMessage(message));
}
logout(): void {
this.transportService.logout(this.modelService.getContactInfo());
this.modelService.setContactInfo(undefined);
this.authService.logout();
}
getDialogInfo(dialogReference: DialogReference): Promise<DialogInfo> {
return this.transportService.getDialogInfo(dialogReference);
}
initializeDialog(dialogReference: DialogReference): void {
this.selectDialogSubject.next(dialogReference);
}
processDialog(contactReference: ContactReference): void {
this.transportService.getOrCreateDialog(this.modelService.getContactInfo().contactReference, contactReference)
.then((dialogInfo: DialogInfo) => {
this.modelService.addDialog(dialogInfo);
this.initializeDialog(dialogInfo.dialogReference);
})
}
sendMessage(dialogReference: DialogReference, value: string): void {
let message: Message = {
dialogReference: dialogReference,
contactInfo: this.modelService.getContactInfo(),
value: value,
date: ""
};
this.transportService.sendMessage(message)
}
}