Skip to content

Commit d46de4a

Browse files
committed
v12.0.0
1 parent 43963dc commit d46de4a

File tree

5 files changed

+130
-48
lines changed

5 files changed

+130
-48
lines changed

demo/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const Gleap = window.Gleap;
22

3-
// Gleap.setFrameUrl("http://0.0.0.0:3001");
4-
// Gleap.setApiUrl("http://0.0.0.0:9000");
5-
6-
Gleap.setDisablePageTracking(true);
3+
Gleap.setFrameUrl("http://0.0.0.0:3001");
4+
Gleap.setApiUrl("http://0.0.0.0:9000");
75

86
Gleap.setLanguage("en");
97

10-
Gleap.initialize("ogWhNhuiZcGWrva5nlDS8l7a78OfaLlV");
8+
Gleap.initialize("X5C0grjFCjUMbZKi131MjZLaGRwg2iKH");
119

1210
/*Gleap.setUrlHandler((url, newTab) => {
1311
alert("URL: " + url + " newTab: " + newTab);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gleap",
3-
"version": "11.2.0",
3+
"version": "12.0.0",
44
"main": "build/index.js",
55
"scripts": {
66
"start": "webpack serve",

src/Gleap.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,14 @@ class Gleap {
155155
* Initializes the SDK
156156
* @param {*} sdkKey
157157
*/
158-
static initialize(sdkKey, disablePing = false) {
158+
static initialize(sdkKey) {
159159
const instance = this.getInstance();
160160
if (instance.initialized) {
161161
console.warn("Gleap already initialized.");
162162
return;
163163
}
164164
instance.initialized = true;
165165

166-
// Stop the ping if needed.
167-
if (disablePing) {
168-
GleapStreamedEvent.getInstance().stop();
169-
}
170-
171166
// Start session
172167
const sessionInstance = GleapSession.getInstance();
173168
sessionInstance.sdkKey = sdkKey;
@@ -177,10 +172,7 @@ class Gleap {
177172
GleapConfigManager.getInstance()
178173
.start()
179174
.then(() => {
180-
if (!disablePing) {
181-
// Inject the Gleap frame.
182-
GleapStreamedEvent.getInstance().start();
183-
}
175+
GleapStreamedEvent.getInstance().start();
184176

185177
runFunctionWhenDomIsReady(() => {
186178
// Inject the widget buttons

src/GleapStreamedEvent.js

Lines changed: 122 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Gleap, { GleapFrameManager, GleapMetaDataManager, GleapSession } from "./Gleap";
12
import { gleapDataParser } from "./GleapHelper";
2-
import Gleap, { GleapSession, GleapNotificationManager, GleapMetaDataManager, GleapFrameManager } from "./Gleap";
3+
4+
const serverUrl = 'ws://localhost:8080';
35

46
export default class GleapStreamedEvent {
57
eventArray = [];
@@ -8,8 +10,99 @@ export default class GleapStreamedEvent {
810
errorCount = 0;
911
streamingEvents = false;
1012
lastUrl = undefined;
11-
stopped = false;
1213
mainLoopTimeout = null;
14+
socket = null;
15+
connectedWebSocketGleapId = null;
16+
connectionTimeout = null;
17+
18+
cleanupWebSocket() {
19+
if (this.connectionTimeout) {
20+
clearTimeout(this.connectionTimeout);
21+
this.connectionTimeout = null;
22+
}
23+
24+
if (this.socket) {
25+
this.socket.onclose = null;
26+
this.socket.onerror = null;
27+
this.socket.onmessage = null;
28+
this.socket.onopen = null;
29+
this.socket.close();
30+
this.socket = null;
31+
}
32+
}
33+
34+
initWebSocket() {
35+
const self = this;
36+
this.connectedWebSocketGleapId = GleapSession.getInstance().session.gleapId;
37+
38+
console.log("Init websocket");
39+
40+
if (!GleapSession.getInstance().session || !GleapSession.getInstance().sdkKey) {
41+
return;
42+
}
43+
44+
this.socket = new WebSocket(`${serverUrl}?gleapId=${GleapSession.getInstance().session.gleapId}&gleapHash=${GleapSession.getInstance().session.gleapHash}&apiKey=${GleapSession.getInstance().sdkKey}&sdkVersion=${SDK_VERSION}`);
45+
46+
// Set a timeout for the connection to open
47+
this.connectionTimeout = setTimeout(() => {
48+
if (self.socket.readyState !== self.socket.OPEN) {
49+
self.socket.close();
50+
console.error('Connection timeout');
51+
52+
GleapStreamedEvent.getInstance().initWebSocket();
53+
}
54+
}, 5000); // Set timeout to 5 seconds
55+
56+
// Event handler for the open event
57+
this.socket.onopen = (event) => {
58+
console.log('Connected to the WebSocket server:', event);
59+
60+
// Clear the connection timeout as the connection is open
61+
if (self.connectionTimeout) {
62+
clearTimeout(self.connectionTimeout);
63+
self.connectionTimeout = null;
64+
}
65+
};
66+
67+
// Event handler for the message event to handle incoming messages
68+
this.socket.onmessage = (event) => {
69+
this.processMessage(JSON.parse(event.data));
70+
};
71+
72+
// Event handler for the error event
73+
this.socket.onerror = (error) => {
74+
console.error('WebSocket Error:', error);
75+
};
76+
77+
// Event handler for the close event
78+
this.socket.onclose = (event) => {
79+
// Check event.wasClean to see if the socket was closed cleanly
80+
if (event.wasClean) {
81+
console.log(`Closed. Reason: ${event.reason} Code: ${event.code}`);
82+
} else {
83+
console.error(`Connection died. Reason: ${event.reason} Code: ${event.code}`);
84+
}
85+
86+
// Attempt to reconnect after a delay
87+
setTimeout(() => {
88+
GleapStreamedEvent.getInstance().initWebSocket();
89+
}, 5000);
90+
};
91+
}
92+
93+
processMessage(message) {
94+
try {
95+
const { a, u } = message;
96+
if (!GleapFrameManager.getInstance().isOpened()) {
97+
if (a) {
98+
Gleap.getInstance().performActions(a);
99+
}
100+
if (u != null) {
101+
GleapNotificationManager.getInstance().setNotificationCount(u);
102+
}
103+
}
104+
} catch (exp) { }
105+
}
13106

14107
// GleapStreamedEvent singleton
15108
static instance;
@@ -29,7 +122,7 @@ export default class GleapStreamedEvent {
29122
}
30123

31124
stop() {
32-
this.stopped = true;
125+
this.cleanupMainLoop();
33126
}
34127

35128
resetErrorCountLoop() {
@@ -38,18 +131,26 @@ export default class GleapStreamedEvent {
38131
}, 60000);
39132
}
40133

41-
restart() {
134+
cleanupMainLoop() {
42135
if (this.mainLoopTimeout) {
43136
clearInterval(this.mainLoopTimeout);
44137
this.mainLoopTimeout = null;
45138
}
139+
}
46140

141+
restart() {
142+
// Only reconnect websockets when needed.
143+
if (this.connectedWebSocketGleapId !== GleapSession.getInstance().session.gleapId) {
144+
this.cleanupWebSocket();
145+
this.initWebSocket();
146+
}
147+
148+
this.cleanupMainLoop();
47149
this.trackInitialEvents();
48150
this.runEventStreamLoop();
49151
}
50152

51153
start() {
52-
this.stopped = false;
53154
this.startPageListener();
54155
this.resetErrorCountLoop();
55156
}
@@ -76,9 +177,6 @@ export default class GleapStreamedEvent {
76177
startPageListener() {
77178
const self = this;
78179
setInterval(function () {
79-
if (self.stopped) {
80-
return;
81-
}
82180
self.logCurrentPage();
83181
}, 1000);
84182
}
@@ -106,27 +204,36 @@ export default class GleapStreamedEvent {
106204
}
107205

108206
runEventStreamLoop = () => {
109-
if (this.stopped) {
110-
return;
111-
}
112-
113207
const self = this;
114208
this.streamEvents();
115209

116210
this.mainLoopTimeout = setTimeout(function () {
117211
self.runEventStreamLoop();
118-
}, 10000);
212+
}, 2000);
119213
};
120214

121215
streamEvents = () => {
122216
if (!GleapSession.getInstance().ready || this.streamingEvents || this.errorCount > 2) {
217+
console.log("Not ready to stream events");
218+
return;
219+
}
220+
221+
// Nothing to stream.
222+
if (this.streamedEventArray.length === 0) {
223+
console.log("Nothing to stream");
224+
return;
225+
}
226+
227+
// Sockets not connected.
228+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
229+
console.log("Socket not connected");
123230
return;
124231
}
125232

126233
const self = this;
127234
this.streamingEvents = true;
128235

129-
const preGleapId = GleapSession.getInstance().getGleapId();
236+
console.log(this.streamedEventArray);
130237

131238
const http = new XMLHttpRequest();
132239
http.open("POST", GleapSession.getInstance().apiUrl + "/sessions/ping");
@@ -140,22 +247,6 @@ export default class GleapStreamedEvent {
140247
if (http.readyState === 4) {
141248
if (http.status === 200 || http.status === 201) {
142249
self.errorCount = 0;
143-
144-
// Only perform actions if gleapId was not changed.
145-
if (GleapSession.getInstance().getGleapId() === preGleapId) {
146-
try {
147-
const response = JSON.parse(http.responseText);
148-
const { a, u } = response;
149-
if (!GleapFrameManager.getInstance().isOpened()) {
150-
if (a) {
151-
Gleap.getInstance().performActions(a);
152-
}
153-
if (u != null) {
154-
GleapNotificationManager.getInstance().setNotificationCount(u);
155-
}
156-
}
157-
} catch (exp) { }
158-
}
159250
} else {
160251
self.errorCount++;
161252
}
@@ -171,6 +262,7 @@ export default class GleapStreamedEvent {
171262
events: this.streamedEventArray,
172263
opened: GleapFrameManager.getInstance().isOpened(),
173264
sdkVersion: SDK_VERSION,
265+
ws: true,
174266
})
175267
);
176268

0 commit comments

Comments
 (0)