Skip to content

Commit b42bc61

Browse files
authored
Merge pull request #28 from GleapSDK/v12.0.0
V12.0.0
2 parents 43963dc + 39bbcfa commit b42bc61

File tree

10 files changed

+151
-60
lines changed

10 files changed

+151
-60
lines changed

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/main.js

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

3-
// Gleap.setFrameUrl("http://0.0.0.0:3001");
4-
// Gleap.setApiUrl("http://0.0.0.0:9000");
3+
// Gleap.setFrameUrl("http://localhost:3001");
4+
// Gleap.setApiUrl("http://localhost:9000");
5+
// Gleap.setWSApiUrl("ws://localhost:8080");
56

6-
Gleap.setDisablePageTracking(true);
7+
// Gleap.setLanguage("en");
78

8-
Gleap.setLanguage("en");
9-
10-
Gleap.initialize("ogWhNhuiZcGWrva5nlDS8l7a78OfaLlV");
9+
Gleap.initialize("X5C0grjFCjUMbZKi131MjZLaGRwg2iKH");
1110

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

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export namespace Gleap {
3939
function destroy(): void;
4040
function isOpened(): boolean;
4141
function setApiUrl(apiUrl: string): void;
42+
function setWSApiUrl(wsApiUrl: string): void;
4243
function setFrameUrl(frameUrl: string): void;
4344
function setBannerUrl(bannerUrl: string): void;
4445
function setMaxNetworkRequests(maxRequests: number): void;

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
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",
@@ -61,4 +61,4 @@
6161
"\\.(css|less)$": "<rootDir>/scripts/testMock.js"
6262
}
6363
}
64-
}
64+
}

published/12.0.0/index.js

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

published/latest/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Gleap.js

Lines changed: 10 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
@@ -404,6 +396,14 @@ class Gleap {
404396
GleapMetaDataManager.setAppBuildNumber(appBuildNumber);
405397
}
406398

399+
/**
400+
* Set a custom ws api url.
401+
* @param {string} wsApiUrl
402+
*/
403+
static setWSApiUrl(wsApiUrl) {
404+
GleapSession.getInstance().wsApiUrl = wsApiUrl;
405+
}
406+
407407
/**
408408
* Set a custom api url.
409409
* @param {string} apiUrl

src/GleapSession.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { eraseGleapCookie, getGleapCookie, loadFromGleapCache, saveToGleapCache,
33

44
export default class GleapSession {
55
apiUrl = "https://api.gleap.io";
6+
wsApiUrl = "wss://ws.gleap.io";
67
sdkKey = null;
78
updatingSession = false;
89
useCookies = false;

src/GleapStreamedEvent.js

Lines changed: 117 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import Gleap, { GleapFrameManager, GleapMetaDataManager, GleapSession } from "./Gleap";
12
import { gleapDataParser } from "./GleapHelper";
2-
import Gleap, { GleapSession, GleapNotificationManager, GleapMetaDataManager, GleapFrameManager } from "./Gleap";
33

44
export default class GleapStreamedEvent {
55
eventArray = [];
@@ -8,8 +8,15 @@ export default class GleapStreamedEvent {
88
errorCount = 0;
99
streamingEvents = false;
1010
lastUrl = undefined;
11-
stopped = false;
1211
mainLoopTimeout = null;
12+
socket = null;
13+
connectedWebSocketGleapId = null;
14+
connectionTimeout = null;
15+
pingWS = null;
16+
handleOpenBound = null;
17+
handleErrorBound = null;
18+
handleMessageBound = null;
19+
handleCloseBound = null;
1320

1421
// GleapStreamedEvent singleton
1522
static instance;
@@ -22,14 +29,99 @@ export default class GleapStreamedEvent {
2229
}
2330
}
2431

25-
constructor() { }
32+
constructor() {
33+
this.handleOpenBound = this.handleOpen.bind(this);
34+
this.handleErrorBound = this.handleError.bind(this);
35+
this.handleMessageBound = this.handleMessage.bind(this);
36+
this.handleCloseBound = this.handleClose.bind(this);
37+
}
38+
39+
cleanupWebSocket() {
40+
if (this.connectionTimeout) {
41+
clearTimeout(this.connectionTimeout);
42+
this.connectionTimeout = null;
43+
}
44+
45+
if (this.pingWS) {
46+
clearInterval(this.pingWS);
47+
}
48+
49+
if (this.socket) {
50+
this.socket.removeEventListener('open', this.handleOpenBound);
51+
this.socket.removeEventListener('error', this.handleErrorBound);
52+
this.socket.removeEventListener('message', this.handleMessageBound);
53+
this.socket.removeEventListener('close', this.handleCloseBound);
54+
this.socket.close();
55+
this.socket = null;
56+
}
57+
}
58+
59+
initWebSocket() {
60+
this.cleanupWebSocket();
61+
62+
this.connectedWebSocketGleapId = GleapSession.getInstance().session.gleapId;
63+
64+
if (!GleapSession.getInstance().session || !GleapSession.getInstance().sdkKey) {
65+
return;
66+
}
67+
68+
this.socket = new WebSocket(`${GleapSession.getInstance().wsApiUrl}?gleapId=${GleapSession.getInstance().session.gleapId}&gleapHash=${GleapSession.getInstance().session.gleapHash}&apiKey=${GleapSession.getInstance().sdkKey}&sdkVersion=${SDK_VERSION}`);
69+
this.socket.addEventListener('open', this.handleOpenBound);
70+
this.socket.addEventListener('message', this.handleMessageBound);
71+
this.socket.addEventListener('error', this.handleErrorBound);
72+
this.socket.addEventListener('close', this.handleCloseBound);
73+
}
74+
75+
handleOpen(event) {
76+
this.pingWS = setInterval(() => {
77+
if (this.socket.readyState === this.socket.OPEN) {
78+
this.socket.send(JSON.stringify({
79+
name: 'ping',
80+
data: {},
81+
}));
82+
}
83+
}, 30000);
84+
85+
if (this.connectionTimeout) {
86+
clearTimeout(this.connectionTimeout);
87+
this.connectionTimeout = null;
88+
}
89+
}
90+
91+
handleMessage(event) {
92+
this.processMessage(JSON.parse(event.data));
93+
}
94+
95+
handleError(error) { }
96+
97+
handleClose(event) {
98+
setTimeout(() => {
99+
this.initWebSocket();
100+
}, 5000);
101+
}
102+
103+
processMessage(message) {
104+
try {
105+
if (message.name === 'update') {
106+
const { a, u } = message.data;
107+
if (!GleapFrameManager.getInstance().isOpened()) {
108+
if (a) {
109+
Gleap.getInstance().performActions(a);
110+
}
111+
if (u != null) {
112+
GleapNotificationManager.getInstance().setNotificationCount(u);
113+
}
114+
}
115+
}
116+
} catch (exp) { }
117+
}
26118

27119
getEventArray() {
28120
return this.eventArray;
29121
}
30122

31123
stop() {
32-
this.stopped = true;
124+
this.cleanupMainLoop();
33125
}
34126

35127
resetErrorCountLoop() {
@@ -38,18 +130,25 @@ export default class GleapStreamedEvent {
38130
}, 60000);
39131
}
40132

41-
restart() {
133+
cleanupMainLoop() {
42134
if (this.mainLoopTimeout) {
43135
clearInterval(this.mainLoopTimeout);
44136
this.mainLoopTimeout = null;
45137
}
138+
}
139+
140+
restart() {
141+
// Only reconnect websockets when needed.
142+
if (this.connectedWebSocketGleapId !== GleapSession.getInstance().session.gleapId) {
143+
this.initWebSocket();
144+
}
46145

146+
this.cleanupMainLoop();
47147
this.trackInitialEvents();
48148
this.runEventStreamLoop();
49149
}
50150

51151
start() {
52-
this.stopped = false;
53152
this.startPageListener();
54153
this.resetErrorCountLoop();
55154
}
@@ -76,9 +175,6 @@ export default class GleapStreamedEvent {
76175
startPageListener() {
77176
const self = this;
78177
setInterval(function () {
79-
if (self.stopped) {
80-
return;
81-
}
82178
self.logCurrentPage();
83179
}, 1000);
84180
}
@@ -106,28 +202,32 @@ export default class GleapStreamedEvent {
106202
}
107203

108204
runEventStreamLoop = () => {
109-
if (this.stopped) {
110-
return;
111-
}
112-
113205
const self = this;
114206
this.streamEvents();
115207

116208
this.mainLoopTimeout = setTimeout(function () {
117209
self.runEventStreamLoop();
118-
}, 10000);
210+
}, 2000);
119211
};
120212

121213
streamEvents = () => {
122214
if (!GleapSession.getInstance().ready || this.streamingEvents || this.errorCount > 2) {
123215
return;
124216
}
125217

218+
// Nothing to stream.
219+
if (this.streamedEventArray.length === 0) {
220+
return;
221+
}
222+
223+
// Sockets not connected.
224+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
225+
return;
226+
}
227+
126228
const self = this;
127229
this.streamingEvents = true;
128230

129-
const preGleapId = GleapSession.getInstance().getGleapId();
130-
131231
const http = new XMLHttpRequest();
132232
http.open("POST", GleapSession.getInstance().apiUrl + "/sessions/ping");
133233
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
@@ -140,22 +240,6 @@ export default class GleapStreamedEvent {
140240
if (http.readyState === 4) {
141241
if (http.status === 200 || http.status === 201) {
142242
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-
}
159243
} else {
160244
self.errorCount++;
161245
}
@@ -171,6 +255,7 @@ export default class GleapStreamedEvent {
171255
events: this.streamedEventArray,
172256
opened: GleapFrameManager.getInstance().isOpened(),
173257
sdkVersion: SDK_VERSION,
258+
ws: true,
174259
})
175260
);
176261

0 commit comments

Comments
 (0)