@@ -172,6 +172,7 @@ class {{ spec.title | caseUcfirst }} {
172172 },
173173 createSocket: () => {
174174 if (this.realtime.channels.size < 1) return;
175+
175176 const channels = new URLSearchParams();
176177 channels.set('project', this.config.project);
177178 this.realtime.channels.forEach(channel => {
@@ -180,20 +181,33 @@ class {{ spec.title | caseUcfirst }} {
180181
181182 const url = this.config.endpointRealtime + '/realtime?' + channels.toString();
182183
183- if (url !== this.realtime.url || !this.realtime.socket || this.realtime.socket?.readyState > WebSocket.OPEN) {
184- if (this.realtime.socket && this.realtime.socket?.readyState < WebSocket.CLOSING) {
184+ if (
185+ url !== this.realtime.url || // Check if URL is present
186+ !this.realtime.socket || // Check if WebSocket has not been created
187+ this.realtime.socket?.readyState > WebSocket.OPEN // Check if WebSocket is CLOSING (3) or CLOSED (4)
188+ ) {
189+ if (
190+ this.realtime.socket &&
191+ this.realtime.socket?.readyState < WebSocket.CLOSING // Close WebSocket if it is CONNECTING (0) or OPEN (1)
192+ ) {
185193 this.realtime.reconnect = false;
186194 this.realtime.socket.close();
187195 }
188196
189197 this.realtime.url = url;
190198 this.realtime.socket = new WebSocket(url);
191199 this.realtime.socket.addEventListener('message', this.realtime.onMessage);
192- this.realtime.socket.addEventListener('open', event => {
200+ this.realtime.socket.addEventListener('open', _event => {
193201 this.realtime.reconnectAttempts = 0;
194202 });
195203 this.realtime.socket.addEventListener('close', event => {
196- if (!this.realtime.reconnect || (this.realtime?.lastMessage?.type === 'error' && (<RealtimeResponseError >this.realtime?.lastMessage.data).code === 1008)) {
204+ if (
205+ !this.realtime.reconnect ||
206+ (
207+ this.realtime?.lastMessage?.type === 'error' && // Check if last message was of type error
208+ (<RealtimeResponseError >this.realtime?.lastMessage.data).code === 1008 // Check for policy violation 1008
209+ )
210+ ) {
197211 this.realtime.reconnect = true;
198212 return;
199213 }
0 commit comments