Skip to content

Commit 39bbcfa

Browse files
committed
v12.0.0
1 parent 9cd4f7d commit 39bbcfa

File tree

10 files changed

+99
-90
lines changed

10 files changed

+99
-90
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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.setLanguage("en");
7+
// Gleap.setLanguage("en");
78

89
Gleap.initialize("X5C0grjFCjUMbZKi131MjZLaGRwg2iKH");
910

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ class Gleap {
396396
GleapMetaDataManager.setAppBuildNumber(appBuildNumber);
397397
}
398398

399+
/**
400+
* Set a custom ws api url.
401+
* @param {string} wsApiUrl
402+
*/
403+
static setWSApiUrl(wsApiUrl) {
404+
GleapSession.getInstance().wsApiUrl = wsApiUrl;
405+
}
406+
399407
/**
400408
* Set a custom api url.
401409
* @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: 69 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import Gleap, { GleapFrameManager, GleapMetaDataManager, GleapSession } from "./Gleap";
22
import { gleapDataParser } from "./GleapHelper";
33

4-
const serverUrl = 'wss://ws.gleap.io';
5-
64
export default class GleapStreamedEvent {
75
eventArray = [];
86
streamedEventArray = [];
@@ -14,109 +12,110 @@ export default class GleapStreamedEvent {
1412
socket = null;
1513
connectedWebSocketGleapId = null;
1614
connectionTimeout = null;
15+
pingWS = null;
16+
handleOpenBound = null;
17+
handleErrorBound = null;
18+
handleMessageBound = null;
19+
handleCloseBound = null;
20+
21+
// GleapStreamedEvent singleton
22+
static instance;
23+
static getInstance() {
24+
if (!this.instance) {
25+
this.instance = new GleapStreamedEvent();
26+
return this.instance;
27+
} else {
28+
return this.instance;
29+
}
30+
}
31+
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+
}
1738

1839
cleanupWebSocket() {
1940
if (this.connectionTimeout) {
2041
clearTimeout(this.connectionTimeout);
2142
this.connectionTimeout = null;
2243
}
2344

45+
if (this.pingWS) {
46+
clearInterval(this.pingWS);
47+
}
48+
2449
if (this.socket) {
25-
this.socket.onclose = null;
26-
this.socket.onerror = null;
27-
this.socket.onmessage = null;
28-
this.socket.onopen = null;
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);
2954
this.socket.close();
3055
this.socket = null;
3156
}
3257
}
3358

3459
initWebSocket() {
35-
const self = this;
36-
this.connectedWebSocketGleapId = GleapSession.getInstance().session.gleapId;
60+
this.cleanupWebSocket();
3761

38-
console.log("Init websocket");
62+
this.connectedWebSocketGleapId = GleapSession.getInstance().session.gleapId;
3963

4064
if (!GleapSession.getInstance().session || !GleapSession.getInstance().sdkKey) {
4165
return;
4266
}
4367

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);
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+
}
5974

60-
// Clear the connection timeout as the connection is open
61-
if (self.connectionTimeout) {
62-
clearTimeout(self.connectionTimeout);
63-
self.connectionTimeout = null;
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+
}));
6482
}
65-
};
83+
}, 30000);
6684

67-
// Event handler for the message event to handle incoming messages
68-
this.socket.onmessage = (event) => {
69-
this.processMessage(JSON.parse(event.data));
70-
};
85+
if (this.connectionTimeout) {
86+
clearTimeout(this.connectionTimeout);
87+
this.connectionTimeout = null;
88+
}
89+
}
7190

72-
// Event handler for the error event
73-
this.socket.onerror = (error) => {
74-
console.error('WebSocket Error:', error);
75-
};
91+
handleMessage(event) {
92+
this.processMessage(JSON.parse(event.data));
93+
}
7694

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-
}
95+
handleError(error) { }
8596

86-
// Attempt to reconnect after a delay
87-
setTimeout(() => {
88-
GleapStreamedEvent.getInstance().initWebSocket();
89-
}, 5000);
90-
};
97+
handleClose(event) {
98+
setTimeout(() => {
99+
this.initWebSocket();
100+
}, 5000);
91101
}
92102

93103
processMessage(message) {
94104
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);
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+
}
102114
}
103115
}
104116
} catch (exp) { }
105117
}
106118

107-
// GleapStreamedEvent singleton
108-
static instance;
109-
static getInstance() {
110-
if (!this.instance) {
111-
this.instance = new GleapStreamedEvent();
112-
return this.instance;
113-
} else {
114-
return this.instance;
115-
}
116-
}
117-
118-
constructor() { }
119-
120119
getEventArray() {
121120
return this.eventArray;
122121
}
@@ -141,7 +140,6 @@ export default class GleapStreamedEvent {
141140
restart() {
142141
// Only reconnect websockets when needed.
143142
if (this.connectedWebSocketGleapId !== GleapSession.getInstance().session.gleapId) {
144-
this.cleanupWebSocket();
145143
this.initWebSocket();
146144
}
147145

@@ -214,27 +212,22 @@ export default class GleapStreamedEvent {
214212

215213
streamEvents = () => {
216214
if (!GleapSession.getInstance().ready || this.streamingEvents || this.errorCount > 2) {
217-
console.log("Not ready to stream events");
218215
return;
219216
}
220217

221218
// Nothing to stream.
222219
if (this.streamedEventArray.length === 0) {
223-
console.log("Nothing to stream");
224220
return;
225221
}
226222

227223
// Sockets not connected.
228224
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
229-
console.log("Socket not connected");
230225
return;
231226
}
232227

233228
const self = this;
234229
this.streamingEvents = true;
235230

236-
console.log(this.streamedEventArray);
237-
238231
const http = new XMLHttpRequest();
239232
http.open("POST", GleapSession.getInstance().apiUrl + "/sessions/ping");
240233
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

0 commit comments

Comments
 (0)