|
|
@@ -41,15 +41,17 @@ export namespace Clients {
|
|
|
|
|
|
private create(id: string): Promise<Interfaces.ClientInterface> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- this.apiConnector.execute('/user', {id: id, action: 'getinfo'}).then((response: string) => {
|
|
|
- try {
|
|
|
- let data = JSON.parse(response);
|
|
|
- this.clients[id] = new Client(this.apiConnector, this, this.conversationsManager, this.logger, data);
|
|
|
- resolve(this.clients[id]);
|
|
|
- } catch (err) {
|
|
|
- reject(err);
|
|
|
- }
|
|
|
- }, reject);
|
|
|
+ this.apiConnector.execute('/user', {id: id, action: 'getinfo'})
|
|
|
+ .then((response: string) => {
|
|
|
+ try {
|
|
|
+ let data = JSON.parse(response);
|
|
|
+ this.clients[id] = new Client(this.apiConnector, this, this.conversationsManager, this.logger, data);
|
|
|
+ resolve(this.clients[id]);
|
|
|
+ } catch (err) {
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(reject);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -59,18 +61,12 @@ export namespace Clients {
|
|
|
client.status = true;
|
|
|
}
|
|
|
|
|
|
- if (!this.clients.hasOwnProperty(data.id)) {
|
|
|
- this.create(data.id).then((client: Client) => {
|
|
|
- this.clients[data.id] = client;
|
|
|
- this.clients[data.id].addSocket(socket);
|
|
|
- setStatus(this.clients[data.id], this.logger);
|
|
|
- }, (error: Error) => {
|
|
|
- console.log(error.message);
|
|
|
- });
|
|
|
- } else {
|
|
|
- this.clients[data.id].addSocket(socket);
|
|
|
- setStatus(this.clients[data.id], this.logger);
|
|
|
- }
|
|
|
+ this.get(data.id).then((client: Client) => {
|
|
|
+ client.addSocket(socket);
|
|
|
+ }, (error: Error) => {
|
|
|
+ console.log(error.message);
|
|
|
+ console.log(error.stack);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
get(id: string, refresh: boolean = false): Promise<Interfaces.ClientInterface> {
|
|
|
@@ -81,7 +77,7 @@ export namespace Clients {
|
|
|
resolve(this.clients[id]);
|
|
|
} else {
|
|
|
this.logger.debug(`Load client with ID=${id} by API call`);
|
|
|
- this.create(id).then((client: Client) => {
|
|
|
+ return this.create(id).then((client: Client) => {
|
|
|
resolve(client);
|
|
|
}, reject);
|
|
|
}
|
|
|
@@ -141,6 +137,7 @@ export namespace Clients {
|
|
|
set status(value: boolean) {
|
|
|
this.api.execute('/user', {id: this.id, action: 'setstatus', value: value});
|
|
|
this.isOnline = value;
|
|
|
+ this.logger.debug(`Status of client with ID=${this.id} is now "${this.isOnline ? 'Online' : 'Offline'}"`);
|
|
|
}
|
|
|
|
|
|
get name() {
|
|
|
@@ -156,18 +153,19 @@ export namespace Clients {
|
|
|
}
|
|
|
|
|
|
constructor(api: Interfaces.ApiConnectorInterface, clientsManager: Interfaces.ClientsManagerInterface, conversationsManager: Interfaces.ConversationsManagerInterface, logger: Interfaces.LoggerInterface, data: MessagingClientData) {
|
|
|
+ this.logger = logger;
|
|
|
+ this.id = data.id;
|
|
|
+ this.logger.debug(`Calling Client.constructor for ID=${this.id}`);
|
|
|
this.api = api;
|
|
|
this.clientsManager = clientsManager;
|
|
|
this.conversationsManager = conversationsManager;
|
|
|
- this.logger = logger;
|
|
|
- this.status = true;
|
|
|
- this.id = data.id;
|
|
|
this.clientPhoto = data.photo;
|
|
|
this.clientName = data.name;
|
|
|
this.payedTime = parseInt(data.payedTime);
|
|
|
this.timeToPay = data.timeToPay;
|
|
|
this._coefficient = parseInt(data.coefficient);
|
|
|
setInterval(this.releaseQueue.bind(this), 10000);
|
|
|
+ this.status = true;
|
|
|
}
|
|
|
|
|
|
private releaseQueue() {
|
|
|
@@ -179,7 +177,6 @@ export namespace Clients {
|
|
|
}
|
|
|
|
|
|
private sendToSockets(event: string, data: {} = {}) {
|
|
|
- this.logger.debug(`Send event "${event}" to client with ID=${this.id}`);
|
|
|
for (let i in this.sockets) {
|
|
|
if (this.sockets.hasOwnProperty(i)) {
|
|
|
(function (i) {
|
|
|
@@ -192,7 +189,9 @@ export namespace Clients {
|
|
|
}
|
|
|
|
|
|
addSocket(socket: SocketIO.Socket) {
|
|
|
+ this.logger.debug(`Add new socket for client with ID=${this.id}`);
|
|
|
this.sockets.push(socket);
|
|
|
+ this.logger.debug(`Client with ID=${this.id} now has ${this.sockets.length} sockets`);
|
|
|
let id = this.sockets.length - 1;
|
|
|
socket.on('disconnect', (data) => {
|
|
|
this.onDisconnect(id);
|
|
|
@@ -205,9 +204,14 @@ export namespace Clients {
|
|
|
}
|
|
|
|
|
|
send(event: string, data: {} = {}) {
|
|
|
+ this.logger.debug(`Send event "${event}" to client with ID=${this.id}`);
|
|
|
+ this.logger.debug(`${this.sockets.length} sockets available for client with ID=${this.id}`);
|
|
|
if (this.sockets.length > 0) {
|
|
|
+ this.logger.debug(`Sending to sockets`);
|
|
|
this.sendToSockets(event, data);
|
|
|
} else {
|
|
|
+ this.logger.debug(`No sockets available for client with ID=${this.id}`);
|
|
|
+ this.logger.debug(`Pushing to queue`);
|
|
|
this.messageQueue.push(new SocketMessage(event, data));
|
|
|
}
|
|
|
}
|
|
|
@@ -221,7 +225,9 @@ export namespace Clients {
|
|
|
}
|
|
|
|
|
|
private onDisconnect(id) {
|
|
|
+ this.logger.debug(`Socket ${id} of client with ID=${this.id} is disconnected`);
|
|
|
this.sockets.splice(id, 1);
|
|
|
+ this.logger.debug(`Client with ID=${this.id} now has ${this.sockets.length} sockets`);
|
|
|
if (this.sockets.length === 0) {
|
|
|
this.status = false;
|
|
|
for (let id in this.conversations) {
|