Multi server connection
This commit is contained in:
@@ -85,14 +85,19 @@ export class VoiceSessionService {
|
||||
navigateToVoiceServer(): void {
|
||||
const session = this._voiceSession();
|
||||
if (session) {
|
||||
// Dispatch joinRoom action to update the store state
|
||||
this.store.dispatch(RoomsActions.joinRoom({
|
||||
roomId: session.serverId,
|
||||
serverInfo: {
|
||||
// Use viewServer to switch view without leaving current server
|
||||
this.store.dispatch(RoomsActions.viewServer({
|
||||
room: {
|
||||
id: session.serverId,
|
||||
name: session.serverName,
|
||||
description: session.serverDescription,
|
||||
hostName: 'Unknown',
|
||||
},
|
||||
hostId: '',
|
||||
isPrivate: false,
|
||||
createdAt: 0,
|
||||
userCount: 0,
|
||||
maxUsers: 50,
|
||||
icon: session.serverIcon,
|
||||
} as any,
|
||||
}));
|
||||
this._isViewingVoiceServer.set(true);
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ export class WebRTCService {
|
||||
private currentServerId: string | null = null;
|
||||
private lastIdentify: { oderId: string; displayName: string } | null = null;
|
||||
private lastJoin: { serverId: string; userId: string } | null = null;
|
||||
// Track all servers the user has joined (for multi-server membership)
|
||||
private joinedServerIds = new Set<string>();
|
||||
|
||||
// Signals for reactive state
|
||||
private readonly _peerId = signal<string>(uuidv4());
|
||||
@@ -190,7 +192,22 @@ export class WebRTCService {
|
||||
displayName: this.lastIdentify.displayName,
|
||||
});
|
||||
}
|
||||
if (this.lastJoin) {
|
||||
// Rejoin ALL servers the user was a member of (multi-server)
|
||||
if (this.joinedServerIds.size > 0) {
|
||||
this.joinedServerIds.forEach((sid) => {
|
||||
this.sendRawMessage({
|
||||
type: 'join_server',
|
||||
serverId: sid,
|
||||
});
|
||||
});
|
||||
// Re-view the last viewed server
|
||||
if (this.lastJoin) {
|
||||
this.sendRawMessage({
|
||||
type: 'view_server',
|
||||
serverId: this.lastJoin.serverId,
|
||||
});
|
||||
}
|
||||
} else if (this.lastJoin) {
|
||||
this.sendRawMessage({
|
||||
type: 'join_server',
|
||||
serverId: this.lastJoin.serverId,
|
||||
@@ -619,8 +636,12 @@ export class WebRTCService {
|
||||
break;
|
||||
|
||||
case 'user_left':
|
||||
this.log('User left', { displayName: message.displayName, oderId: message.oderId });
|
||||
this.removePeer(message.oderId);
|
||||
this.log('User left', { displayName: message.displayName, oderId: message.oderId, serverId: message.serverId });
|
||||
// With multi-server membership, only remove peer if they are leaving the
|
||||
// currently viewed server AND we don't share any other server with them.
|
||||
// For now, don't remove peers on server-specific leave –
|
||||
// the signaling server will send a proper disconnect when the WS closes.
|
||||
// The store effect handles removing the user from the user list.
|
||||
break;
|
||||
|
||||
case 'offer':
|
||||
@@ -1162,9 +1183,10 @@ export class WebRTCService {
|
||||
});
|
||||
}
|
||||
|
||||
// Join a room
|
||||
// Join a room (first-time join – adds to multi-server membership)
|
||||
joinRoom(roomId: string, userId: string): void {
|
||||
this.lastJoin = { serverId: roomId, userId };
|
||||
this.joinedServerIds.add(roomId);
|
||||
this.sendRawMessage({
|
||||
type: 'join_server',
|
||||
serverId: roomId,
|
||||
@@ -1172,23 +1194,30 @@ export class WebRTCService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch to a different server without disconnecting voice or peers.
|
||||
* This is used when navigating between servers while already connected.
|
||||
* If voice is connected, the user stays in voice in the original server.
|
||||
* Switch the viewed server without affecting multi-server membership.
|
||||
* If the server hasn't been joined yet, performs a full join.
|
||||
* If already joined, just sends view_server to refresh user list.
|
||||
*/
|
||||
switchServer(serverId: string, userId: string): void {
|
||||
// Update the last join info for reconnection purposes
|
||||
this.lastJoin = { serverId, userId };
|
||||
|
||||
// Send join_server to switch rooms on the signaling server
|
||||
// This tells the server we're now in a different room, but doesn't
|
||||
// affect our peer connections or voice state
|
||||
this.sendRawMessage({
|
||||
type: 'join_server',
|
||||
serverId: serverId,
|
||||
});
|
||||
|
||||
this.log('Switched server', { serverId, userId, voiceConnected: this._isVoiceConnected() });
|
||||
if (this.joinedServerIds.has(serverId)) {
|
||||
// Already a member – just switch the view
|
||||
this.sendRawMessage({
|
||||
type: 'view_server',
|
||||
serverId: serverId,
|
||||
});
|
||||
this.log('Viewed server (already joined)', { serverId, userId, voiceConnected: this._isVoiceConnected() });
|
||||
} else {
|
||||
// Not yet joined – do a full join
|
||||
this.joinedServerIds.add(serverId);
|
||||
this.sendRawMessage({
|
||||
type: 'join_server',
|
||||
serverId: serverId,
|
||||
});
|
||||
this.log('Joined new server via switch', { serverId, userId, voiceConnected: this._isVoiceConnected() });
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
@@ -1329,12 +1358,37 @@ export class WebRTCService {
|
||||
}
|
||||
}
|
||||
|
||||
// Leave room
|
||||
leaveRoom(): void {
|
||||
this.sendRawMessage({
|
||||
type: 'leave_server',
|
||||
});
|
||||
// Leave a specific server (or all if no serverId given)
|
||||
leaveRoom(serverId?: string): void {
|
||||
if (serverId) {
|
||||
// Leave a specific server
|
||||
this.joinedServerIds.delete(serverId);
|
||||
this.sendRawMessage({
|
||||
type: 'leave_server',
|
||||
serverId: serverId,
|
||||
});
|
||||
this.log('Left server', { serverId });
|
||||
|
||||
// If no servers left, do full cleanup
|
||||
if (this.joinedServerIds.size === 0) {
|
||||
this.fullCleanup();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave ALL servers and clean up
|
||||
this.joinedServerIds.forEach((sid) => {
|
||||
this.sendRawMessage({
|
||||
type: 'leave_server',
|
||||
serverId: sid,
|
||||
});
|
||||
});
|
||||
this.joinedServerIds.clear();
|
||||
this.fullCleanup();
|
||||
}
|
||||
|
||||
// Internal full cleanup – close peers, stop media
|
||||
private fullCleanup(): void {
|
||||
// Clear all P2P reconnect timers
|
||||
this.clearAllP2PReconnectTimers();
|
||||
|
||||
@@ -1353,9 +1407,19 @@ export class WebRTCService {
|
||||
this.stopScreenShare();
|
||||
}
|
||||
|
||||
/** Check whether we are already a member of the given server */
|
||||
hasJoinedServer(serverId: string): boolean {
|
||||
return this.joinedServerIds.has(serverId);
|
||||
}
|
||||
|
||||
/** Get all server IDs we are currently a member of */
|
||||
getJoinedServerIds(): ReadonlySet<string> {
|
||||
return this.joinedServerIds;
|
||||
}
|
||||
|
||||
// Disconnect from signaling server
|
||||
disconnect(): void {
|
||||
this.leaveRoom();
|
||||
this.leaveRoom(); // leaves all servers
|
||||
this.stopVoiceHeartbeat();
|
||||
|
||||
if (this.signalingSocket) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { lucidePlus } from '@ng-icons/lucide';
|
||||
import { Room } from '../../core/models';
|
||||
import { selectSavedRooms, selectCurrentRoom } from '../../store/rooms/rooms.selectors';
|
||||
import { VoiceSessionService } from '../../core/services/voice-session.service';
|
||||
import { WebRTCService } from '../../core/services/webrtc.service';
|
||||
import * as RoomsActions from '../../store/rooms/rooms.actions';
|
||||
|
||||
@Component({
|
||||
@@ -21,6 +22,7 @@ export class ServersRailComponent {
|
||||
private store = inject(Store);
|
||||
private router = inject(Router);
|
||||
private voiceSession = inject(VoiceSessionService);
|
||||
private webrtc = inject(WebRTCService);
|
||||
savedRooms = this.store.selectSignal(selectSavedRooms);
|
||||
currentRoom = this.store.selectSignal(selectCurrentRoom);
|
||||
|
||||
@@ -52,8 +54,6 @@ export class ServersRailComponent {
|
||||
|
||||
joinSavedRoom(room: Room): void {
|
||||
// Require auth: if no current user, go to login
|
||||
const current = this.currentRoom();
|
||||
// currentRoom presence does not indicate auth; check localStorage for currentUserId
|
||||
const currentUserId = localStorage.getItem('metoyou_currentUserId');
|
||||
if (!currentUserId) {
|
||||
this.router.navigate(['/login']);
|
||||
@@ -64,21 +64,28 @@ export class ServersRailComponent {
|
||||
const voiceServerId = this.voiceSession.getVoiceServerId();
|
||||
if (voiceServerId && voiceServerId !== room.id) {
|
||||
// User is switching to a different server while connected to voice
|
||||
// Update voice session to show floating controls
|
||||
// Update voice session to show floating controls (voice stays connected)
|
||||
this.voiceSession.setViewingVoiceServer(false);
|
||||
} else if (voiceServerId === room.id) {
|
||||
// Navigating back to the voice-connected server
|
||||
this.voiceSession.setViewingVoiceServer(true);
|
||||
}
|
||||
|
||||
this.store.dispatch(RoomsActions.joinRoom({
|
||||
roomId: room.id,
|
||||
serverInfo: {
|
||||
name: room.name,
|
||||
description: room.description,
|
||||
hostName: room.hostId || 'Unknown',
|
||||
},
|
||||
}));
|
||||
// If we've already joined this server, just switch the view
|
||||
// (no user_joined broadcast, no leave from other servers)
|
||||
if (this.webrtc.hasJoinedServer(room.id)) {
|
||||
this.store.dispatch(RoomsActions.viewServer({ room }));
|
||||
} else {
|
||||
// First time joining this server
|
||||
this.store.dispatch(RoomsActions.joinRoom({
|
||||
roomId: room.id,
|
||||
serverInfo: {
|
||||
name: room.name,
|
||||
description: room.description,
|
||||
hostName: room.hostId || 'Unknown',
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
openContextMenu(evt: MouseEvent, room: Room): void {
|
||||
|
||||
@@ -67,6 +67,17 @@ export const leaveRoom = createAction('[Rooms] Leave Room');
|
||||
|
||||
export const leaveRoomSuccess = createAction('[Rooms] Leave Room Success');
|
||||
|
||||
// View server (switch view without disconnecting)
|
||||
export const viewServer = createAction(
|
||||
'[Rooms] View Server',
|
||||
props<{ room: Room }>()
|
||||
);
|
||||
|
||||
export const viewServerSuccess = createAction(
|
||||
'[Rooms] View Server Success',
|
||||
props<{ room: Room }>()
|
||||
);
|
||||
|
||||
// Delete room
|
||||
export const deleteRoom = createAction(
|
||||
'[Rooms] Delete Room',
|
||||
|
||||
@@ -196,9 +196,8 @@ export class RoomsEffects {
|
||||
|
||||
// Check if already connected to signaling server
|
||||
if (this.webrtc.isConnected()) {
|
||||
// Already connected - just switch to the new server without reconnecting WebSocket
|
||||
// This preserves voice connections and peer state
|
||||
console.log('Already connected to signaling, switching to room:', room.id);
|
||||
// Already connected - join the new server (additive, multi-server)
|
||||
console.log('Already connected to signaling, joining room:', room.id);
|
||||
this.webrtc.setCurrentServer(room.id);
|
||||
this.webrtc.switchServer(room.id, oderId);
|
||||
} else {
|
||||
@@ -223,6 +222,37 @@ export class RoomsEffects {
|
||||
{ dispatch: false }
|
||||
);
|
||||
|
||||
// View server – switch view to an already-joined server without leaving others
|
||||
viewServer$ = createEffect(() =>
|
||||
this.actions$.pipe(
|
||||
ofType(RoomsActions.viewServer),
|
||||
withLatestFrom(this.store.select(selectCurrentUser)),
|
||||
mergeMap(([{ room }, user]) => {
|
||||
const oderId = user?.oderId || this.webrtc.peerId();
|
||||
|
||||
if (this.webrtc.isConnected()) {
|
||||
this.webrtc.setCurrentServer(room.id);
|
||||
this.webrtc.switchServer(room.id, oderId);
|
||||
}
|
||||
|
||||
this.router.navigate(['/room', room.id]);
|
||||
return of(RoomsActions.viewServerSuccess({ room }));
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
// When viewing a different server, reload messages and users for that server
|
||||
onViewServerSuccess$ = createEffect(() =>
|
||||
this.actions$.pipe(
|
||||
ofType(RoomsActions.viewServerSuccess),
|
||||
mergeMap(({ room }) => [
|
||||
UsersActions.clearUsers(),
|
||||
MessagesActions.loadMessages({ roomId: room.id }),
|
||||
UsersActions.loadBans(),
|
||||
])
|
||||
)
|
||||
);
|
||||
|
||||
// Leave room
|
||||
leaveRoom$ = createEffect(() =>
|
||||
this.actions$.pipe(
|
||||
@@ -280,10 +310,8 @@ export class RoomsEffects {
|
||||
// Delete from local DB
|
||||
this.db.deleteRoom(roomId);
|
||||
|
||||
// If currently in this room, disconnect
|
||||
if (currentRoom?.id === roomId) {
|
||||
this.webrtc.disconnectAll();
|
||||
}
|
||||
// Leave this specific server (doesn't affect other servers)
|
||||
this.webrtc.leaveRoom(roomId);
|
||||
|
||||
return of(RoomsActions.forgetRoomSuccess({ roomId }));
|
||||
})
|
||||
@@ -451,12 +479,23 @@ export class RoomsEffects {
|
||||
// Listen to WebRTC signaling messages for user presence
|
||||
signalingMessages$ = createEffect(() =>
|
||||
this.webrtc.onSignalingMessage.pipe(
|
||||
withLatestFrom(this.store.select(selectCurrentUser)),
|
||||
mergeMap(([message, currentUser]: [any, any]) => {
|
||||
withLatestFrom(
|
||||
this.store.select(selectCurrentUser),
|
||||
this.store.select(selectCurrentRoom),
|
||||
),
|
||||
mergeMap(([message, currentUser, currentRoom]: [any, any, any]) => {
|
||||
const actions: any[] = [];
|
||||
const myId = currentUser?.oderId || currentUser?.id;
|
||||
const viewedServerId = currentRoom?.id;
|
||||
|
||||
if (message.type === 'server_users' && message.users) {
|
||||
// Only populate for the currently viewed server
|
||||
const msgServerId = message.serverId;
|
||||
if (msgServerId && viewedServerId && msgServerId !== viewedServerId) {
|
||||
return [{ type: 'NO_OP' }];
|
||||
}
|
||||
// Clear existing users first, then add the new set
|
||||
actions.push(UsersActions.clearUsers());
|
||||
// Add all existing users to the store (excluding ourselves)
|
||||
message.users.forEach((user: { oderId: string; displayName: string }) => {
|
||||
// Don't add ourselves to the list
|
||||
@@ -478,6 +517,11 @@ export class RoomsEffects {
|
||||
}
|
||||
});
|
||||
} else if (message.type === 'user_joined') {
|
||||
// Only add to user list if this event is for the currently viewed server
|
||||
const msgServerId = message.serverId;
|
||||
if (msgServerId && viewedServerId && msgServerId !== viewedServerId) {
|
||||
return [{ type: 'NO_OP' }];
|
||||
}
|
||||
// Don't add ourselves to the list
|
||||
if (message.oderId !== myId) {
|
||||
actions.push(
|
||||
@@ -496,6 +540,11 @@ export class RoomsEffects {
|
||||
);
|
||||
}
|
||||
} else if (message.type === 'user_left') {
|
||||
// Only remove from user list if this event is for the currently viewed server
|
||||
const msgServerId = message.serverId;
|
||||
if (msgServerId && viewedServerId && msgServerId !== viewedServerId) {
|
||||
return [{ type: 'NO_OP' }];
|
||||
}
|
||||
actions.push(UsersActions.userLeft({ userId: message.oderId }));
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +121,20 @@ export const roomsReducer = createReducer(
|
||||
isConnected: false,
|
||||
})),
|
||||
|
||||
// View server – just switch the viewed room, stay connected
|
||||
on(RoomsActions.viewServer, (state) => ({
|
||||
...state,
|
||||
isConnecting: true,
|
||||
error: null,
|
||||
})),
|
||||
|
||||
on(RoomsActions.viewServerSuccess, (state, { room }) => ({
|
||||
...state,
|
||||
currentRoom: room,
|
||||
isConnecting: false,
|
||||
isConnected: true,
|
||||
})),
|
||||
|
||||
// Update room settings
|
||||
on(RoomsActions.updateRoomSettings, (state) => ({
|
||||
...state,
|
||||
|
||||
Reference in New Issue
Block a user