Socket upgrade.

This commit is contained in:
retoor 2025-12-29 01:25:28 +01:00
parent 163828e213
commit 56d0ef01fa
2 changed files with 90 additions and 60 deletions

View File

@ -83,6 +83,7 @@ class SocketService(BaseService):
self.users = {} self.users = {}
self.subscriptions = {} self.subscriptions = {}
self.last_update = str(datetime.now()) self.last_update = str(datetime.now())
self._lock = asyncio.Lock()
async def user_availability_service(self): async def user_availability_service(self):
@ -139,6 +140,7 @@ class SocketService(BaseService):
username = safe_get(user, "username", "unknown") username = safe_get(user, "username", "unknown")
nick = safe_get(user, "nick") or username nick = safe_get(user, "nick") or username
color = s.user_color color = s.user_color
async with self._lock:
self.sockets.add(s) self.sockets.add(s)
is_first_connection = False is_first_connection = False
if user_uid not in self.users: if user_uid not in self.users:
@ -166,6 +168,7 @@ class SocketService(BaseService):
if not ws or not channel_uid or not user_uid: if not ws or not channel_uid or not user_uid:
return False return False
try: try:
async with self._lock:
existing_socket = None existing_socket = None
user_sockets = self.users.get(user_uid, set()) user_sockets = self.users.get(user_uid, set())
for sock in user_sockets: for sock in user_sockets:
@ -183,6 +186,24 @@ class SocketService(BaseService):
logger.warning(f"Failed to subscribe: {safe_str(ex)}") logger.warning(f"Failed to subscribe: {safe_str(ex)}")
return False return False
async def unsubscribe(self, ws, channel_uid, user_uid):
if not ws or not channel_uid or not user_uid:
return False
try:
async with self._lock:
if channel_uid in self.subscriptions:
self.subscriptions[channel_uid].discard(user_uid)
if len(self.subscriptions[channel_uid]) == 0:
del self.subscriptions[channel_uid]
for s in self.sockets:
if s and s.ws == ws:
s.subscribed_channels.discard(channel_uid)
break
return True
except Exception as ex:
logger.warning(f"Failed to unsubscribe: {safe_str(ex)}")
return False
async def send_to_user(self, user_uid, message): async def send_to_user(self, user_uid, message):
if not user_uid or message is None: if not user_uid or message is None:
return 0 return 0
@ -250,6 +271,7 @@ class SocketService(BaseService):
async def delete(self, ws): async def delete(self, ws):
if not ws: if not ws:
return return
async with self._lock:
sockets_to_remove = [sock for sock in self.sockets if sock and sock.ws == ws] sockets_to_remove = [sock for sock in self.sockets if sock and sock.ws == ws]
for s in sockets_to_remove: for s in sockets_to_remove:
self.sockets.discard(s) self.sockets.discard(s)

View File

@ -157,12 +157,10 @@ class MessageList extends HTMLElement {
threshold: 0, threshold: 0,
}); });
// End-of-messages marker
this.endOfMessages = document.createElement('div'); this.endOfMessages = document.createElement('div');
this.endOfMessages.classList.add('message-list-bottom'); this.endOfMessages.classList.add('message-list-bottom');
this.prepend(this.endOfMessages); this.prepend(this.endOfMessages);
// Observe existing children and index by uid
for (const c of this.children) { for (const c of this.children) {
this._observer.observe(c); this._observer.observe(c);
if (c instanceof MessageElement) { if (c instanceof MessageElement) {
@ -170,20 +168,30 @@ class MessageList extends HTMLElement {
} }
} }
// Wire up socket events this._updateMessageHandler = (data) => {
app.ws.addEventListener("update_message_text", (data) => {
if (this.messageMap.has(data.uid)) { if (this.messageMap.has(data.uid)) {
this.upsertMessage(data); this.upsertMessage(data);
} }
}); };
app.ws.addEventListener("set_typing", (data) => { this._typingHandler = (data) => {
if (app._debug) console.debug("set_typing event received:", data); if (app._debug) console.debug("set_typing event received:", data);
this.triggerGlow(data.user_uid, data.color); this.triggerGlow(data.user_uid, data.color);
}); };
app.ws.addEventListener("update_message_text", this._updateMessageHandler);
app.ws.addEventListener("set_typing", this._typingHandler);
this.scrollToBottom(true); this.scrollToBottom(true);
} }
disconnectedCallback() {
if (this._observer) {
this._observer.disconnect();
}
app.ws.removeEventListener("update_message_text", this._updateMessageHandler);
app.ws.removeEventListener("set_typing", this._typingHandler);
}
connectedCallback() { connectedCallback() {
this.addEventListener('click', (e) => { this.addEventListener('click', (e) => {
if ( if (