v7.0.0-beta.1 release
This commit is contained in:
commit
6595fd0eb5
3967 files changed
+1875383
No files matched your search
@@ -0,0 +1,310 @@
|
||||
/// <reference types="vite/client" />
|
||||
import Echo from 'laravel-echo';
|
||||
import Pusher from 'pusher-js';
|
||||
import { createLogger } from '../utils/Logger';
|
||||
|
||||
// Pusher를 window 객체에 등록 (Laravel Echo 필수)
|
||||
declare global {
|
||||
interface Window {
|
||||
Pusher: typeof Pusher;
|
||||
Echo: Echo<'reverb'>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocket 채널 타입
|
||||
*/
|
||||
export type ChannelType = 'public' | 'private' | 'presence';
|
||||
|
||||
/**
|
||||
* WebSocket 구독 옵션
|
||||
*/
|
||||
export interface SubscriptionOptions {
|
||||
/** 채널 타입 (기본값: 'private') */
|
||||
channelType?: ChannelType;
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocket 설정 인터페이스 (Blade에서 동적으로 전달)
|
||||
*/
|
||||
export interface WebSocketConfig {
|
||||
/** Reverb 앱 키 */
|
||||
appKey: string;
|
||||
/** WebSocket 호스트 (기본값: localhost) */
|
||||
host?: string;
|
||||
/** WebSocket 포트 (기본값: 80) */
|
||||
port?: number;
|
||||
/** 스키마 (http 또는 https, 기본값: https) */
|
||||
scheme?: 'http' | 'https';
|
||||
/** Private 채널 인증 엔드포인트 (기본값: /broadcasting/auth) */
|
||||
authEndpoint?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* WebSocket 연결을 관리하는 싱글톤 매니저
|
||||
*
|
||||
* Laravel Reverb와 Laravel Echo를 사용하여 WebSocket 연결을 관리합니다.
|
||||
* 설정은 Blade 템플릿에서 동적으로 전달받습니다.
|
||||
*/
|
||||
const logger = createLogger('WebSocketManager');
|
||||
|
||||
class WebSocketManager {
|
||||
private echo: Echo<'reverb'> | null = null;
|
||||
private subscriptions: Map<string, ReturnType<Echo<'reverb'>['channel']>> = new Map();
|
||||
private initialized = false;
|
||||
private config: WebSocketConfig | null = null;
|
||||
|
||||
/**
|
||||
* WebSocket 설정을 지정합니다.
|
||||
* TemplateApp 초기화 시 호출됩니다.
|
||||
*
|
||||
* @param config WebSocket 설정
|
||||
*/
|
||||
configure(config: WebSocketConfig): void {
|
||||
if (this.initialized) {
|
||||
logger.warn('[WebSocketManager] 이미 초기화되어 설정을 변경할 수 없습니다.');
|
||||
return;
|
||||
}
|
||||
this.config = config;
|
||||
logger.log('[WebSocketManager] 설정 완료:', {
|
||||
appKey: config.appKey ? '***' : '(없음)',
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
scheme: config.scheme,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Echo 인스턴스를 초기화합니다.
|
||||
*/
|
||||
initialize(): void {
|
||||
if (this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 설정 확인
|
||||
if (!this.config || !this.config.appKey) {
|
||||
logger.warn('[WebSocketManager] WebSocket 설정이 없습니다. initTemplateApp에서 websocket 옵션을 전달해주세요.');
|
||||
return;
|
||||
}
|
||||
|
||||
const { appKey, host = 'localhost', port = 80, scheme = 'https', authEndpoint = '/api/broadcasting/auth' } = this.config;
|
||||
const numPort = Number(port) || 80;
|
||||
const useTLS = scheme === 'https';
|
||||
|
||||
logger.log('[WebSocketManager] 연결 설정:', { host, port: numPort, scheme, useTLS, authEndpoint });
|
||||
|
||||
window.Pusher = Pusher;
|
||||
|
||||
// Pusher 클라이언트를 직접 생성하여 전달
|
||||
// Pusher 8.x에서는 enabledTransports/disabledTransports 설정 시 충돌 가능
|
||||
// Sanctum 토큰 가져오기
|
||||
const authToken = localStorage.getItem('auth_token');
|
||||
|
||||
const pusherOptions = {
|
||||
wsHost: host,
|
||||
wsPort: numPort,
|
||||
wssPort: numPort,
|
||||
forceTLS: useTLS,
|
||||
disableStats: true,
|
||||
enabledTransports: ['ws', 'wss'] as const,
|
||||
cluster: 'mt1', // 필수 옵션이지만 Reverb에서는 무시됨
|
||||
// Private 채널 인증 설정
|
||||
authEndpoint: authEndpoint,
|
||||
auth: {
|
||||
headers: {
|
||||
'Authorization': authToken ? `Bearer ${authToken}` : '',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
logger.log('[WebSocketManager] Pusher 옵션:', pusherOptions);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const pusherClient = new Pusher(appKey, pusherOptions as any);
|
||||
|
||||
// 연결 상태 디버깅을 위한 이벤트 리스너
|
||||
pusherClient.connection.bind('connecting', () => {
|
||||
logger.log('[WebSocketManager] 연결 시도 중...');
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('connected', () => {
|
||||
logger.log('[WebSocketManager] 연결 성공! Socket ID:', pusherClient.connection.socket_id);
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('failed', () => {
|
||||
logger.error('[WebSocketManager] 연결 실패 - WebSocket을 사용할 수 없습니다.');
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('error', (error: unknown) => {
|
||||
logger.error('[WebSocketManager] 연결 오류:', error);
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('state_change', (states: { previous: string; current: string }) => {
|
||||
logger.log(`[WebSocketManager] 상태 변경: ${states.previous} → ${states.current}`);
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('unavailable', () => {
|
||||
logger.error('[WebSocketManager] WebSocket 사용 불가 - 연결할 수 없습니다.');
|
||||
});
|
||||
|
||||
pusherClient.connection.bind('disconnected', () => {
|
||||
logger.warn('[WebSocketManager] 연결이 끊어졌습니다.');
|
||||
});
|
||||
|
||||
// 초기 연결 상태 로깅
|
||||
logger.log('[WebSocketManager] 초기 연결 상태:', pusherClient.connection.state);
|
||||
|
||||
this.echo = new Echo({
|
||||
broadcaster: 'reverb',
|
||||
client: pusherClient,
|
||||
});
|
||||
|
||||
// 명시적으로 연결 시작
|
||||
logger.log('[WebSocketManager] 연결 시작 시도...');
|
||||
pusherClient.connect();
|
||||
|
||||
window.Echo = this.echo;
|
||||
this.initialized = true;
|
||||
logger.log('[WebSocketManager] Echo 초기화 완료');
|
||||
}
|
||||
|
||||
/**
|
||||
* 채널을 구독하고 이벤트를 리스닝합니다.
|
||||
*
|
||||
* @param channel 채널명 (예: 'admin.dashboard')
|
||||
* @param event 이벤트명 (예: 'dashboard.stats.updated')
|
||||
* @param callback 이벤트 수신 시 호출될 콜백
|
||||
* @param options 구독 옵션
|
||||
* @returns 구독 키
|
||||
*/
|
||||
subscribe(
|
||||
channel: string,
|
||||
event: string,
|
||||
callback: (data: unknown) => void,
|
||||
options: SubscriptionOptions = {}
|
||||
): string {
|
||||
this.initialize();
|
||||
|
||||
if (!this.echo) {
|
||||
logger.warn('[WebSocketManager] Echo가 초기화되지 않았습니다.');
|
||||
return '';
|
||||
}
|
||||
|
||||
const { channelType = 'private' } = options;
|
||||
const subscriptionKey = `${channel}:${event}`;
|
||||
|
||||
if (this.subscriptions.has(subscriptionKey)) {
|
||||
logger.log(`[WebSocketManager] 이미 구독 중: ${subscriptionKey}`);
|
||||
return subscriptionKey;
|
||||
}
|
||||
|
||||
let channelInstance: ReturnType<Echo<'reverb'>['channel']>;
|
||||
switch (channelType) {
|
||||
case 'public':
|
||||
channelInstance = this.echo.channel(channel);
|
||||
break;
|
||||
case 'presence':
|
||||
channelInstance = this.echo.join(channel);
|
||||
break;
|
||||
default:
|
||||
channelInstance = this.echo.private(channel);
|
||||
}
|
||||
|
||||
// Reverb 이벤트는 .으로 시작해야 함
|
||||
channelInstance.listen(`.${event}`, callback);
|
||||
this.subscriptions.set(subscriptionKey, channelInstance);
|
||||
|
||||
logger.log(`[WebSocketManager] 구독 완료: ${subscriptionKey} (${channelType})`);
|
||||
|
||||
return subscriptionKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 구독을 해제합니다.
|
||||
*
|
||||
* @param subscriptionKey 구독 키
|
||||
*/
|
||||
unsubscribe(subscriptionKey: string): void {
|
||||
if (this.subscriptions.has(subscriptionKey)) {
|
||||
this.subscriptions.delete(subscriptionKey);
|
||||
logger.log(`[WebSocketManager] 구독 해제: ${subscriptionKey}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 채널의 모든 구독을 해제합니다.
|
||||
*
|
||||
* @param channel 채널명
|
||||
*/
|
||||
leaveChannel(channel: string): void {
|
||||
if (!this.echo) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.echo.leave(channel);
|
||||
|
||||
// 해당 채널의 모든 구독 키 제거
|
||||
const keysToDelete: string[] = [];
|
||||
this.subscriptions.forEach((_, key) => {
|
||||
if (key.startsWith(`${channel}:`)) {
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
keysToDelete.forEach((key) => this.subscriptions.delete(key));
|
||||
logger.log(`[WebSocketManager] 채널 구독 해제: ${channel}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 모든 구독을 해제하고 연결을 종료합니다.
|
||||
*/
|
||||
disconnect(): void {
|
||||
if (this.echo) {
|
||||
this.echo.disconnect();
|
||||
this.subscriptions.clear();
|
||||
this.initialized = false;
|
||||
logger.log('[WebSocketManager] 연결 종료');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Echo 인스턴스를 반환합니다.
|
||||
*
|
||||
* @returns Echo 인스턴스 또는 null
|
||||
*/
|
||||
getEcho(): Echo<'reverb'> | null {
|
||||
return this.echo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 초기화 여부를 반환합니다.
|
||||
*
|
||||
* @returns 초기화 여부
|
||||
*/
|
||||
isInitialized(): boolean {
|
||||
return this.initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 설정 여부를 반환합니다.
|
||||
*
|
||||
* @returns 설정 여부
|
||||
*/
|
||||
isConfigured(): boolean {
|
||||
return this.config !== null && !!this.config.appKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 현재 활성 구독 수를 반환합니다.
|
||||
*
|
||||
* @returns 구독 수
|
||||
*/
|
||||
getSubscriptionCount(): number {
|
||||
return this.subscriptions.size;
|
||||
}
|
||||
}
|
||||
|
||||
export const webSocketManager = new WebSocketManager();
|
||||
export { WebSocketManager };
|
||||
@@ -0,0 +1,267 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
// Logger 모킹
|
||||
vi.mock('../../utils/Logger', () => ({
|
||||
Logger: {
|
||||
getInstance: () => ({
|
||||
debug: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
info: vi.fn(),
|
||||
log: vi.fn(),
|
||||
isDebugEnabled: () => false,
|
||||
}),
|
||||
},
|
||||
createLogger: () => ({
|
||||
log: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Echo와 Pusher 모킹
|
||||
const mockListen = vi.fn().mockReturnThis();
|
||||
const mockChannel = vi.fn().mockReturnValue({ listen: mockListen });
|
||||
const mockPrivate = vi.fn().mockReturnValue({ listen: mockListen });
|
||||
const mockJoin = vi.fn().mockReturnValue({ listen: mockListen });
|
||||
const mockLeave = vi.fn();
|
||||
const mockDisconnect = vi.fn();
|
||||
|
||||
// Mock Echo 클래스
|
||||
class MockEcho {
|
||||
channel = mockChannel;
|
||||
private = mockPrivate;
|
||||
join = mockJoin;
|
||||
leave = mockLeave;
|
||||
disconnect = mockDisconnect;
|
||||
}
|
||||
|
||||
vi.mock('laravel-echo', () => ({
|
||||
default: MockEcho,
|
||||
}));
|
||||
|
||||
// Mock Pusher 클래스 with connection
|
||||
const mockConnectionBind = vi.fn();
|
||||
const mockConnect = vi.fn();
|
||||
|
||||
vi.mock('pusher-js', () => ({
|
||||
default: class MockPusher {
|
||||
connection = {
|
||||
bind: mockConnectionBind,
|
||||
state: 'initialized',
|
||||
socket_id: 'test-socket-id',
|
||||
};
|
||||
connect = mockConnect;
|
||||
},
|
||||
}));
|
||||
|
||||
describe('WebSocketManager', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 모듈 캐시 초기화 (싱글톤 리셋)
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
/**
|
||||
* WebSocketManager에 설정을 적용하고 반환하는 헬퍼 함수
|
||||
*/
|
||||
async function getConfiguredManager() {
|
||||
const { webSocketManager } = await import('../WebSocketManager');
|
||||
webSocketManager.configure({
|
||||
appKey: 'test-key',
|
||||
host: 'localhost',
|
||||
port: 8080,
|
||||
scheme: 'http',
|
||||
});
|
||||
return webSocketManager;
|
||||
}
|
||||
|
||||
describe('configure', () => {
|
||||
it('should set configuration correctly', async () => {
|
||||
const { webSocketManager } = await import('../WebSocketManager');
|
||||
|
||||
expect(webSocketManager.isConfigured()).toBe(false);
|
||||
|
||||
webSocketManager.configure({
|
||||
appKey: 'test-key',
|
||||
host: 'localhost',
|
||||
port: 8080,
|
||||
scheme: 'http',
|
||||
});
|
||||
|
||||
expect(webSocketManager.isConfigured()).toBe(true);
|
||||
});
|
||||
|
||||
it('should not allow reconfiguration after initialization', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
// 초기화 트리거
|
||||
webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
expect(webSocketManager.isInitialized()).toBe(true);
|
||||
|
||||
// 재설정 시도 (무시되어야 함)
|
||||
webSocketManager.configure({
|
||||
appKey: 'new-key',
|
||||
host: 'new-host',
|
||||
port: 9090,
|
||||
scheme: 'https',
|
||||
});
|
||||
|
||||
// 여전히 초기화된 상태 유지
|
||||
expect(webSocketManager.isInitialized()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('subscribe', () => {
|
||||
it('should create subscription key correctly', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
const key = webSocketManager.subscribe(
|
||||
'admin.dashboard',
|
||||
'dashboard.stats.updated',
|
||||
callback
|
||||
);
|
||||
|
||||
expect(key).toBe('admin.dashboard:dashboard.stats.updated');
|
||||
});
|
||||
|
||||
it('should not duplicate subscriptions for same channel:event', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback1 = vi.fn();
|
||||
const callback2 = vi.fn();
|
||||
|
||||
const key1 = webSocketManager.subscribe('test.channel', 'test.event', callback1);
|
||||
const key2 = webSocketManager.subscribe('test.channel', 'test.event', callback2);
|
||||
|
||||
expect(key1).toBe(key2);
|
||||
});
|
||||
|
||||
it('should use private channel by default', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
webSocketManager.subscribe('admin.dashboard', 'test.event', callback);
|
||||
|
||||
expect(mockPrivate).toHaveBeenCalledWith('admin.dashboard');
|
||||
});
|
||||
|
||||
it('should use public channel when specified', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
webSocketManager.subscribe('public.channel', 'test.event', callback, {
|
||||
channelType: 'public',
|
||||
});
|
||||
|
||||
expect(mockChannel).toHaveBeenCalledWith('public.channel');
|
||||
});
|
||||
|
||||
it('should use presence channel when specified', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
webSocketManager.subscribe('presence.channel', 'test.event', callback, {
|
||||
channelType: 'presence',
|
||||
});
|
||||
|
||||
expect(mockJoin).toHaveBeenCalledWith('presence.channel');
|
||||
});
|
||||
|
||||
it('should listen with dot prefix for event name', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
|
||||
expect(mockListen).toHaveBeenCalledWith('.test.event', callback);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unsubscribe', () => {
|
||||
it('should handle non-existent subscription gracefully', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
|
||||
expect(() => {
|
||||
webSocketManager.unsubscribe('non.existent:key');
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('disconnect', () => {
|
||||
it('should call echo disconnect', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
// 초기화를 위해 subscribe 호출
|
||||
webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
|
||||
webSocketManager.disconnect();
|
||||
|
||||
expect(mockDisconnect).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should reset initialized state after disconnect', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
expect(webSocketManager.isInitialized()).toBe(true);
|
||||
|
||||
webSocketManager.disconnect();
|
||||
expect(webSocketManager.isInitialized()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSubscriptionCount', () => {
|
||||
it('should return correct subscription count', async () => {
|
||||
const webSocketManager = await getConfiguredManager();
|
||||
const callback = vi.fn();
|
||||
|
||||
expect(webSocketManager.getSubscriptionCount()).toBe(0);
|
||||
|
||||
webSocketManager.subscribe('channel1', 'event1', callback);
|
||||
expect(webSocketManager.getSubscriptionCount()).toBe(1);
|
||||
|
||||
webSocketManager.subscribe('channel2', 'event2', callback);
|
||||
expect(webSocketManager.getSubscriptionCount()).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should not initialize without configuration', async () => {
|
||||
const { webSocketManager } = await import('../WebSocketManager');
|
||||
const callback = vi.fn();
|
||||
|
||||
// configure 호출 안함
|
||||
const key = webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
|
||||
expect(key).toBe('');
|
||||
expect(webSocketManager.isInitialized()).toBe(false);
|
||||
expect(webSocketManager.isConfigured()).toBe(false);
|
||||
});
|
||||
|
||||
it('should not initialize with empty appKey', async () => {
|
||||
const { webSocketManager } = await import('../WebSocketManager');
|
||||
|
||||
webSocketManager.configure({
|
||||
appKey: '',
|
||||
host: 'localhost',
|
||||
port: 8080,
|
||||
scheme: 'http',
|
||||
});
|
||||
|
||||
const callback = vi.fn();
|
||||
const key = webSocketManager.subscribe('test.channel', 'test.event', callback);
|
||||
|
||||
expect(key).toBe('');
|
||||
expect(webSocketManager.isInitialized()).toBe(false);
|
||||
expect(webSocketManager.isConfigured()).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* WebSocket 모듈 내보내기
|
||||
*/
|
||||
export { webSocketManager, WebSocketManager } from './WebSocketManager';
|
||||
export type { ChannelType, SubscriptionOptions } from './WebSocketManager';
|
||||
Reference in new issue
Block a user