force reload on rejoin (for warm reload), handle leave messages directly from go client unregistering
This commit is contained in:
parent
e199d421f7
commit
47983c7fe3
19
hub.go
19
hub.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"radchat/server"
|
||||
@ -154,12 +155,30 @@ func Run(h *server.Hub) {
|
||||
SendUsersList(h)
|
||||
|
||||
case client := <-h.Unregister:
|
||||
var messageJson []byte
|
||||
messageJson, _ = json.Marshal(Message{
|
||||
Type: "system_message",
|
||||
Data: fmt.Sprintf("%s left the voice chat", client.Username),
|
||||
DataType: "system_message_leave",
|
||||
// FIX: Please...
|
||||
// Using CONFIG.APP.SYSTEM_MSG_DEFAULT_TIMEOUT from javascript config
|
||||
// Hard coding it here for now
|
||||
DataTime: 5000,
|
||||
})
|
||||
|
||||
for cli := range client.Hub.Clients {
|
||||
SendToClient(client.Hub, cli.Id, messageJson)
|
||||
}
|
||||
|
||||
h.Mutex.Lock()
|
||||
if _, ok := h.Clients[client]; ok {
|
||||
delete(h.Clients, client)
|
||||
close(client.Send)
|
||||
}
|
||||
h.Mutex.Unlock()
|
||||
|
||||
log.Println("Client left. Client ID:", client.Id)
|
||||
|
||||
SendUsersList(h)
|
||||
|
||||
case message := <-h.Broadcast:
|
||||
|
@ -1,6 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
@ -9,5 +12,7 @@ type Client struct {
|
||||
Username string
|
||||
Id string
|
||||
Hub *Hub
|
||||
Mu sync.RWMutex
|
||||
LastPong time.Time
|
||||
Send chan []byte
|
||||
}
|
||||
|
@ -872,6 +872,22 @@ class WebSocketManager {
|
||||
WebSocketManager.setupEventHandlers();
|
||||
}
|
||||
|
||||
static ping() {
|
||||
if (state.ws?.readyState !== WebSocket.OPEN) {
|
||||
return;
|
||||
}
|
||||
const buffer = new ArrayBuffer(7);
|
||||
const dataView = new DataView(buffer);
|
||||
dataView.setInt8(0, 0x89);
|
||||
dataView.setInt8(1, 0x00);
|
||||
dataView.setInt8(2, 0x48);
|
||||
dataView.setInt8(3, 0x65);
|
||||
dataView.setInt8(4, 0x6c);
|
||||
dataView.setInt8(5, 0x6c);
|
||||
dataView.setInt8(6, 0x6f);
|
||||
state.ws.send(buffer);
|
||||
}
|
||||
|
||||
static setupEventHandlers() {
|
||||
state.ws.onopen = () => {
|
||||
UIManager.updateConnectionStatus(true);
|
||||
@ -999,6 +1015,8 @@ class WebSocketManager {
|
||||
const rejoinInterval = setInterval(async () => {
|
||||
if (state.ws && state.ws.readyState === WebSocket.OPEN) {
|
||||
clearInterval(rejoinInterval);
|
||||
// Force a refresh to load changes
|
||||
window.location.reload();
|
||||
console.log('🔄 Reconnected, rejoining voice chat...');
|
||||
if (!state.localStream || state.localStream.getTracks().length === 0 ||
|
||||
state.localStream.getAudioTracks().every(track => track.readyState !== 'live')) {
|
||||
@ -1384,12 +1402,12 @@ class UserManager {
|
||||
}
|
||||
|
||||
static onVoiceChatLeave() {
|
||||
WebSocketManager.sendSystemMessage(`${state.currentUsername} left the voice chat`,
|
||||
'except',
|
||||
[state.currentId],
|
||||
'system_message_leave',
|
||||
CONFIG.APP.SYSTEM_MSG_DEFAULT_TIMEOUT,
|
||||
)
|
||||
// WebSocketManager.sendSystemMessage(`${state.currentUsername} left the voice chat`,
|
||||
// 'except',
|
||||
// [state.currentId],
|
||||
// 'system_message_leave',
|
||||
// CONFIG.APP.SYSTEM_MSG_DEFAULT_TIMEOUT,
|
||||
// )
|
||||
Utils.playSound('/sounds/leave.wav', state.apparentOutputVolume());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user