-
-
Notifications
You must be signed in to change notification settings - Fork 18
Socket connection #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Socket connection #308
Changes from all commits
028a55e
9fc21bf
4bc18a8
f4bb343
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| package com.yogeshpaliyal.deepr.analytics | ||
|
|
||
| import android.content.Context | ||
| import com.yogeshpaliyal.shared.analytics.AnalyticsManager | ||
| import com.yogeshpaliyal.shared.analytics.AnalyticsManagerFactory | ||
|
|
||
| object AnalyticsManagerFactoryImpl : AnalyticsManagerFactory { | ||
| override fun create(context: Context): AnalyticsManager = NoOpAnalyticsManager() | ||
| override fun create(): AnalyticsManager = NoOpAnalyticsManager() | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -268,8 +268,83 @@ <h3 class="text-xl font-semibold text-gray-700 mb-2">Loading links...</h3> | |||||
| <script> | ||||||
| // Global variables to store all links and available tags | ||||||
| let allLinks = []; | ||||||
| let availableTags = []; // Now contains {id, name, count} objects | ||||||
| let selectedTags = []; // Now contains {id, name} objects | ||||||
| let availableTags = []; | ||||||
| let selectedTags = []; | ||||||
| let linksSocket = null; | ||||||
| let reconnectTimer = null; | ||||||
| let hasConnectedToSocket = false; | ||||||
| const WS_RETRY_MS = 5000; | ||||||
|
|
||||||
| // WebSocket connection management | ||||||
| function connectLinksWebSocket() { | ||||||
| const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'; | ||||||
| const wsUrl = `${protocol}://${window.location.host}/ws/updates`; | ||||||
|
|
||||||
| try { | ||||||
| linksSocket?.close(); | ||||||
| linksSocket = new WebSocket(wsUrl); | ||||||
| } catch (error) { | ||||||
| console.error('Unable to create websocket', error); | ||||||
| scheduleSocketReconnect(); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| linksSocket.addEventListener('open', () => { | ||||||
| hasConnectedToSocket = true; | ||||||
| if (reconnectTimer) { | ||||||
| clearTimeout(reconnectTimer); | ||||||
| reconnectTimer = null; | ||||||
| } | ||||||
| showToast('Live updates enabled', 'info'); | ||||||
| }); | ||||||
|
|
||||||
| linksSocket.addEventListener('message', (event) => handleSocketMessage(event.data)); | ||||||
|
|
||||||
| linksSocket.addEventListener('close', () => { | ||||||
| if (hasConnectedToSocket) { | ||||||
| showToast('Live updates disconnected. Retrying...', 'error'); | ||||||
| } | ||||||
| scheduleSocketReconnect(); | ||||||
| }); | ||||||
|
|
||||||
| linksSocket.addEventListener('error', () => { | ||||||
| linksSocket?.close(); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| function handleSocketMessage(rawData) { | ||||||
| try { | ||||||
| const parsed = JSON.parse(rawData); | ||||||
| switch(parsed.dataType){ | ||||||
| case 'links_list': | ||||||
| allLinks = parsed.links; | ||||||
| populateTagFilter(); | ||||||
| filterAndDisplayLinks(); | ||||||
| break; | ||||||
| default: | ||||||
| console.warn('Unknown dataType received from websocket:', parsed.dataType); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| console.error('Failed parsing websocket payload', error); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function scheduleSocketReconnect() { | ||||||
| if (reconnectTimer) { | ||||||
| return; | ||||||
| } | ||||||
| reconnectTimer = setTimeout(() => { | ||||||
| reconnectTimer = null; | ||||||
| connectLinksWebSocket(); | ||||||
| }, WS_RETRY_MS); | ||||||
| } | ||||||
|
|
||||||
| function disconnectWebSocket() { | ||||||
| if (websocket) { | ||||||
| websocket.close(); | ||||||
| websocket = null; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Toast notification system | ||||||
| function showToast(message, type = 'success') { | ||||||
|
|
@@ -676,7 +751,13 @@ <h3 class="text-xl font-semibold text-gray-700 mb-2">Error loading links</h3> | |||||
| tagsInput.value = ''; | ||||||
| selectedTags = []; | ||||||
| updateSelectedTagsDisplay(); | ||||||
| loadLinks(); | ||||||
|
|
||||||
| // If WebSocket is not connected, fallback to HTTP | ||||||
| if (!websocket || websocket.readyState !== WebSocket.OPEN) { | ||||||
|
||||||
| if (!websocket || websocket.readyState !== WebSocket.OPEN) { | |
| if (!linksSocket || linksSocket.readyState !== WebSocket.OPEN) { |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
References undefined websocket variable instead of linksSocket. Should use linksSocket to check WebSocket connection state.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.yogeshpaliyal.deepr.data | ||
|
|
||
| import com.yogeshpaliyal.deepr.GetLinksAndTags | ||
| import com.yogeshpaliyal.shared.data.DeeprLink | ||
|
|
||
| fun GetLinksAndTags.toDeeprLink(): DeeprLink { | ||
| return DeeprLink( | ||
| id = this.id, | ||
| link = this.link, | ||
| name = this.name, | ||
| createdAt = this.createdAt, | ||
| openedCount = this.openedCount, | ||
| isFavourite = this.isFavourite, | ||
| notes = this.notes, | ||
| thumbnail = this.thumbnail, | ||
| lastOpenedAt = this.lastOpenedAt, | ||
| tagsNames = this.tagsNames, | ||
| tagsIds = this.tagsIds, | ||
| tags = this.tagsNames?.split(",")?.filter { it.isNotEmpty() } ?: emptyList(), | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function references
websocketvariable, but the code useslinksSocketas the variable name. This should belinksSocketto match the actual WebSocket variable being used throughout the file.