Add ServerState support in ChatService with SSE listener and associated data model
This commit is contained in:
parent
65d0118e3d
commit
2705cf14ab
|
|
@ -0,0 +1,14 @@
|
|||
export interface User {
|
||||
uuid: string;
|
||||
name: string;
|
||||
styledName: string;
|
||||
}
|
||||
|
||||
export interface Server {
|
||||
name: string;
|
||||
players: User[];
|
||||
}
|
||||
|
||||
export interface ServerState {
|
||||
servers: Server[];
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import {precomputeNode} from '@pages/altitude/chat/mini-message/mini-message.uti
|
|||
import {ChatChannel} from '@pages/altitude/chat/objects/chat-channel.object';
|
||||
import {NotificationService} from '@pages/altitude/chat/service/chat-notification.service';
|
||||
import {ChatInfoService} from '@api';
|
||||
import {ServerState} from '@pages/altitude/chat/objects/server-state.object';
|
||||
|
||||
interface SsePayloadEvent {
|
||||
data: string;
|
||||
|
|
@ -24,6 +25,8 @@ export class ChatService implements OnDestroy {
|
|||
public readonly channels = computed(() => this._channels().sort((a, b) => a.name.localeCompare(b.name)));
|
||||
private readonly _selectedChannel = signal<ChatChannel | null>(null);
|
||||
public readonly selectedChannel = this._selectedChannel.asReadonly()
|
||||
private readonly _serverState = signal<ServerState | null>(null);
|
||||
public readonly serverState = this._serverState.asReadonly();
|
||||
public readonly partieMap = signal<Map<string, string>>(new Map());
|
||||
public readonly userNameMap = signal<Map<string, string>>(new Map());
|
||||
private readonly pendingPartyNameRequests = new Set<string>();
|
||||
|
|
@ -78,6 +81,11 @@ export class ChatService implements OnDestroy {
|
|||
});
|
||||
});
|
||||
|
||||
this.on(source, 'server-state', (event) => {
|
||||
const state = JSON.parse(event.data) as ServerState;
|
||||
this._serverState.set(state);
|
||||
});
|
||||
|
||||
source.onerror = (err) => {
|
||||
console.error('SSE error, polyfill will auto-reconnect:', err);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user