Skip to content

Commit 385d396

Browse files
committed
wait for socket to be ready before sending messages
1 parent 630fd51 commit 385d396

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

resources/js/socket-worker.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ export default class SocketWorker {
8080
this.boot();
8181
}
8282

83+
@action
84+
private connected() {
85+
this.connectionStatus = 'connected';
86+
this.hasConnectedOnce = true;
87+
}
88+
8389
@action
8490
private connectWebSocket() {
8591
if (!this.active || this.endpoint == null || this.ws != null) {
@@ -97,11 +103,6 @@ export default class SocketWorker {
97103

98104
const token = tokenEl.getAttribute('content');
99105
this.ws = new WebSocket(`${this.endpoint}?csrf=${token}`);
100-
this.ws.addEventListener('open', action(() => {
101-
this.retryDelay.reset();
102-
this.connectionStatus = 'connected';
103-
this.hasConnectedOnce = true;
104-
}));
105106
this.ws.addEventListener('close', this.reconnectWebSocket);
106107
this.ws.addEventListener('message', this.handleNewEvent);
107108
}
@@ -127,7 +128,9 @@ export default class SocketWorker {
127128
const eventData = this.parseMessageEvent(event);
128129
if (eventData == null) return;
129130

130-
if (isNotificationEventLogoutJson(eventData)) {
131+
if (eventData.event === 'connection.ready') {
132+
this.connected();
133+
} else if (isNotificationEventLogoutJson(eventData)) {
131134
this.destroy();
132135
core.userLoginObserver.logout();
133136
} else if (isNotificationEventVerifiedJson(eventData)) {

0 commit comments

Comments
 (0)