forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvorlon.clientMessenger.ts
192 lines (162 loc) · 7.11 KB
/
vorlon.clientMessenger.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module VORLON {
import Socket = SocketIO.Socket;
declare var io;
export class ClientMessenger {
private _socket: Socket;
private _isConnected = false;
private _sessionId: string;
private _clientId: string;
private _listenClientId: string;
private _serverUrl: string;
private _waitingEvents: number;
public onRealtimeMessageReceived: (pluginID: string, receivedObject: any) => void;
public onHeloReceived: (id: string) => void;
public onIdentifyReceived: (id: string) => void;
public onWaitingEventsReceived: (id: string, waitingevents: number) => void;
public onStopListenReceived: () => void;
public onRefreshClients: () => void;
public onError: (err: Error) => void;
public get isConnected(): boolean {
return this._isConnected;
}
public set clientId(value: string) {
this._clientId = value;
}
public get socketId(): string {
return this._socket.id;
}
constructor(side: RuntimeSide, serverUrl: string, sessionId: string, clientId: string, listenClientId: string) {
this._isConnected = false;
this._sessionId = sessionId;
this._clientId = clientId;
Core._listenClientId = listenClientId;
this._serverUrl = serverUrl;
this._waitingEvents = 0;
switch (side) {
case RuntimeSide.Client:
this._socket = io.connect(serverUrl);
this._isConnected = true;
break;
case RuntimeSide.Dashboard:
this._socket = io.connect(serverUrl + "/dashboard");
this._isConnected = true;
break;
}
if (this.isConnected) {
var manager = io.Manager(serverUrl);
manager.on('connect_error',(err) => {
if (this.onError) {
this.onError(err);
}
});
this._socket.on('message', message => {
var receivedObject = JSON.parse(message);
var pluginID = receivedObject._pluginID;
delete receivedObject._pluginID;
if (this.onRealtimeMessageReceived) {
this.onRealtimeMessageReceived(pluginID, receivedObject);
}
});
this._socket.on('helo', message => {
Core._listenClientId = message;
if (this.onHeloReceived) {
this.onHeloReceived(message);
}
});
this._socket.on('identify', message => {
if (this.onIdentifyReceived) {
this.onIdentifyReceived(message);
}
});
this._socket.on('stoplisten',() => {
if (this.onStopListenReceived) {
this.onStopListenReceived();
}
});
this._socket.on('waitingevents', message => {
if (this.onWaitingEventsReceived) {
var receivedObject = JSON.parse(message);
this.onWaitingEventsReceived(receivedObject._clientId, receivedObject._waitingEvents);
}
});
this._socket.on('refreshclients',() => {
if (this.onRefreshClients) {
this.onRefreshClients();
}
});
}
}
public sendWaitingEvents(pluginID: string, waitingevents: number): void {
var objectToSend: any = {};
objectToSend._pluginID = pluginID;
objectToSend._side = RuntimeSide.Client;
objectToSend._sessionId = this._sessionId;
objectToSend._clientId = this._clientId;
objectToSend._listenClientId = Core._listenClientId;
objectToSend._waitingEvents = waitingevents;
if (this.isConnected) {
var message = JSON.stringify(objectToSend);
this._socket.emit("waitingevents", message);
}
}
public sendRealtimeMessage(pluginID: string, objectToSend: any, side: RuntimeSide, messageType = "message", incrementVisualIndicator = false): void {
objectToSend._pluginID = pluginID;
objectToSend._side = side;
objectToSend._sessionId = this._sessionId;
objectToSend._clientId = this._clientId;
objectToSend._listenClientId = Core._listenClientId;
if (!this.isConnected) {
// Directly raise response locally
if (this.onRealtimeMessageReceived) {
this.onRealtimeMessageReceived(pluginID, objectToSend);
}
return;
} else {
if (Core._listenClientId === "" && messageType === "message") {
if (incrementVisualIndicator) {
this._waitingEvents++;
this.sendWaitingEvents(pluginID, this._waitingEvents);
}
} else {
var message = JSON.stringify(objectToSend);
this._socket.emit(messageType, message);
this._waitingEvents = 0;
this.sendWaitingEvents(pluginID, 0);
}
}
}
public sendMonitoringMessage(pluginID: string, message: string): void {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
}
}
}
xhr.open("POST", this._serverUrl + "api/push");
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
var data = JSON.stringify({ "_idsession": this._sessionId, "id": pluginID, "message": message });
//xhr.setRequestHeader("Content-length", data.length.toString());
xhr.send(data);
}
public getMonitoringMessage(pluginID: string, onMonitoringMessage: (messages: string[]) => any, from = "-20", to = "-1"): any {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (onMonitoringMessage)
onMonitoringMessage(<string[]>JSON.parse(xhr.responseText));
} else {
if (onMonitoringMessage)
onMonitoringMessage(null);
}
} else {
if (onMonitoringMessage)
onMonitoringMessage(null);
}
};
xhr.open("GET", this._serverUrl + "api/range/" + this._sessionId + "/" + pluginID + "/" + from + "/" + to);
xhr.send();
}
}
}