forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvorlon.core.ts
174 lines (143 loc) · 6.38 KB
/
vorlon.core.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
module VORLON {
export class Core {
static _plugins = new Array<Plugin>();
static _messenger: ClientMessenger;
static _sessionID: string;
static _listenClientId: string;
static _side: RuntimeSide;
static _RetryTimeout = 1002;
public static get Messenger(): ClientMessenger {
return Core._messenger;
}
public static get Plugins(): Array<Plugin> {
return Core._plugins;
}
public static RegisterPlugin(plugin: Plugin): void {
Core._plugins.push(plugin);
}
public static Start(serverUrl = "'http://localhost:1337/'", sessionId = "", listenClientId = "", divMapper: (string) => HTMLDivElement = null): void {
Core._side = RuntimeSide.Client;
Core._sessionID = sessionId;
Core._listenClientId = listenClientId;
if (!serverUrl) {
Core._side = RuntimeSide.Both;
}
if (divMapper) {
Core._side = RuntimeSide.Dashboard;
}
// Cookie
var clientId = Tools.ReadCookie("vorlonJS_clientId");
if (!clientId) {
clientId = Tools.CreateGUID();
Tools.CreateCookie("vorlonJS_clientId", clientId, 1);
}
// Creating the messenger
Core._messenger = new ClientMessenger(Core._side, serverUrl, sessionId, clientId, listenClientId);
// Connect messenger to dispatcher
Core.Messenger.onRealtimeMessageReceived = Core._Dispatch;
Core.Messenger.onHeloReceived = Core._OnIdentificationReceived;
Core.Messenger.onIdentifyReceived = Core._OnIdentifyReceived;
Core.Messenger.onStopListenReceived = Core._OnStopListenReceived;
Core.Messenger.onError = Core._OnError;
// Say 'helo'
var heloMessage = {
ua: navigator.userAgent
};
Core.Messenger.sendRealtimeMessage("", heloMessage, Core._side, "helo");
// Launch plugins
for (var index = 0; index < Core._plugins.length; index++) {
var plugin = Core._plugins[index];
if (Core._side === RuntimeSide.Both || Core._side === RuntimeSide.Client) {
plugin.startClientSide();
}
if (Core._side === RuntimeSide.Both || Core._side === RuntimeSide.Dashboard) {
plugin.startDashboardSide(divMapper ? divMapper(plugin.getID()) : null);
}
}
if (Core._side === RuntimeSide.Client) { // handle client disconnection
window.addEventListener("beforeunload", function () {
Core.Messenger.sendRealtimeMessage("", { socketid: Core.Messenger.socketId }, Core._side, "clientclosed");
}, false);
}
}
private static _OnStopListenReceived(): void {
Core._listenClientId = "";
}
private static _OnIdentifyReceived(message: string): void {
var div = document.createElement("div");
div.style.position = "absolute";
div.style.left = "0";
div.style.top = "50%";
div.style.marginTop = "-150px";
div.style.width = "100%";
div.style.height = "300px";
div.style.fontFamily = "Arial";
div.style.fontSize = "300px";
div.style.textAlign = "center";
div.style.color = "white";
div.style.textShadow = "2px 2px 5px black";
div.innerHTML = message;
document.body.appendChild(div);
setTimeout(() => {
document.body.removeChild(div);
}, 4000);
}
private static _OnError(err: Error): void {
var divError = document.createElement("div");
divError.style.position = "absolute";
divError.style.top = "0";
divError.style.left = "0";
divError.style.width = "100%";
divError.style.height = "100px";
divError.style.backgroundColor = "red";
divError.style.textAlign = "center";
divError.style.fontSize = "30px";
divError.style.paddingTop = "20px";
divError.style.color = "white";
divError.style.fontFamily = "consolas";
divError.innerHTML = "Error while connecting to server. Server may be offline.<BR>Error message: " + err.message;
document.body.appendChild(divError);
setTimeout(() => {
document.body.removeChild(divError);
}, 5000);
}
private static _OnIdentificationReceived(id: string): void {
Core._listenClientId = id;
if (Core._side === RuntimeSide.Client) {
// Refresh plugins
for (var index = 0; index < Core._plugins.length; index++) {
var plugin = Core._plugins[index];
plugin.refresh();
}
}
}
private static _RetrySendingRealtimeMessage(plugin: Plugin, receivedObject: any) {
setTimeout(() => {
if (plugin.isReady()) {
plugin.onRealtimeMessageReceivedFromClientSide(receivedObject);
return;
}
Core._RetrySendingRealtimeMessage(plugin, receivedObject);
}, Core._RetryTimeout);
}
private static _Dispatch(pluginID: string, receivedObject: any): void {
var side = receivedObject._side;
delete receivedObject._side;
for (var index = 0; index < Core._plugins.length; index++) {
var plugin = Core._plugins[index];
if (plugin.getID() === pluginID) {
if (side === RuntimeSide.Client) {
if (!plugin.isReady()) { // Plugin is not ready, let's try again later
Core._RetrySendingRealtimeMessage(plugin, receivedObject);
} else {
plugin.onRealtimeMessageReceivedFromClientSide(receivedObject);
}
} else {
plugin.onRealtimeMessageReceivedFromDashboardSide(receivedObject);
}
return;
}
}
}
}
}