@daeryun/dr_components 0.6.51 → 0.6.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daeryun/dr_components",
3
- "version": "0.6.51",
3
+ "version": "0.6.52",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,22 +0,0 @@
1
- type SocketEventHandlers = {
2
- onConnect?: () => void;
3
- onDisconnect?: () => void;
4
- onMessage?: (data: any) => void;
5
- onError?: (error: Event) => void;
6
- };
7
- declare class SocketManager {
8
- private socket;
9
- private url;
10
- private handlers;
11
- private reconnectAttempts;
12
- private maxReconnectAttempts;
13
- private reconnectTimeout;
14
- constructor(url: string, handlers?: SocketEventHandlers);
15
- connect(): void;
16
- disconnect(): void;
17
- send(data: any): boolean;
18
- private attemptReconnect;
19
- isConnected(): boolean;
20
- updateHandlers(handlers: SocketEventHandlers): void;
21
- }
22
- export default SocketManager;
@@ -1,88 +0,0 @@
1
- class SocketManager {
2
- constructor(url, handlers = {}) {
3
- this.socket = null;
4
- this.reconnectAttempts = 0;
5
- this.maxReconnectAttempts = 5;
6
- this.reconnectTimeout = null;
7
- this.url = url;
8
- this.handlers = handlers;
9
- }
10
- connect() {
11
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
12
- return;
13
- }
14
- try {
15
- this.socket = new WebSocket(this.url);
16
- this.socket.onopen = () => {
17
- this.reconnectAttempts = 0;
18
- if (this.handlers.onConnect) {
19
- this.handlers.onConnect();
20
- }
21
- };
22
- this.socket.onmessage = event => {
23
- if (this.handlers.onMessage) {
24
- this.handlers.onMessage(event);
25
- }
26
- };
27
- this.socket.onclose = () => {
28
- if (this.handlers.onDisconnect) {
29
- this.handlers.onDisconnect();
30
- }
31
- this.attemptReconnect();
32
- };
33
- this.socket.onerror = error => {
34
- if (this.handlers.onError) {
35
- this.handlers.onError(error);
36
- }
37
- };
38
- }
39
- catch (e) {
40
- console.error('소켓 연결 오류:', e);
41
- this.attemptReconnect();
42
- }
43
- }
44
- disconnect() {
45
- if (this.reconnectTimeout) {
46
- clearTimeout(this.reconnectTimeout);
47
- this.reconnectTimeout = null;
48
- }
49
- if (this.socket) {
50
- this.socket.close();
51
- this.socket = null;
52
- }
53
- }
54
- send(data) {
55
- if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
56
- return false;
57
- }
58
- try {
59
- const message = typeof data === 'string' ? data : JSON.stringify(data);
60
- this.socket.send(message);
61
- return true;
62
- }
63
- catch (e) {
64
- console.error('메시지 전송 오류:', e);
65
- return false;
66
- }
67
- }
68
- attemptReconnect() {
69
- if (this.reconnectAttempts >= this.maxReconnectAttempts) {
70
- console.error('최대 재연결 시도 횟수 도달');
71
- return;
72
- }
73
- this.reconnectAttempts++;
74
- // 지수 백오프 적용
75
- const timeout = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
76
- this.reconnectTimeout = setTimeout(() => {
77
- console.log(`재연결 시도 중... 시도 ${this.reconnectAttempts}`);
78
- this.connect();
79
- }, timeout);
80
- }
81
- isConnected() {
82
- return this.socket !== null && this.socket.readyState === WebSocket.OPEN;
83
- }
84
- updateHandlers(handlers) {
85
- this.handlers = Object.assign(Object.assign({}, this.handlers), handlers);
86
- }
87
- }
88
- export default SocketManager;
@@ -1,115 +0,0 @@
1
- type SocketEventHandlers = {
2
- onConnect?: () => void;
3
- onDisconnect?: () => void;
4
- onMessage?: (data: any) => void;
5
- onError?: (error: Event) => void;
6
- };
7
-
8
- class SocketManager {
9
- private socket: WebSocket | null = null;
10
- private url: string;
11
- private handlers: SocketEventHandlers;
12
- private reconnectAttempts: number = 0;
13
- private maxReconnectAttempts: number = 5;
14
- private reconnectTimeout: NodeJS.Timeout | null = null;
15
-
16
- constructor(url: string, handlers: SocketEventHandlers = {}) {
17
- this.url = url;
18
- this.handlers = handlers;
19
- }
20
-
21
- connect(): void {
22
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
23
- return;
24
- }
25
-
26
- try {
27
- this.socket = new WebSocket(this.url);
28
-
29
- this.socket.onopen = () => {
30
- this.reconnectAttempts = 0;
31
-
32
- if (this.handlers.onConnect) {
33
- this.handlers.onConnect();
34
- }
35
- };
36
-
37
- this.socket.onmessage = event => {
38
- if (this.handlers.onMessage) {
39
- this.handlers.onMessage(event);
40
- }
41
- };
42
-
43
- this.socket.onclose = () => {
44
- if (this.handlers.onDisconnect) {
45
- this.handlers.onDisconnect();
46
- }
47
-
48
- this.attemptReconnect();
49
- };
50
-
51
- this.socket.onerror = error => {
52
- if (this.handlers.onError) {
53
- this.handlers.onError(error);
54
- }
55
- };
56
- } catch (e) {
57
- console.error('소켓 연결 오류:', e);
58
- this.attemptReconnect();
59
- }
60
- }
61
-
62
- disconnect(): void {
63
- if (this.reconnectTimeout) {
64
- clearTimeout(this.reconnectTimeout);
65
- this.reconnectTimeout = null;
66
- }
67
-
68
- if (this.socket) {
69
- this.socket.close();
70
- this.socket = null;
71
- }
72
- }
73
-
74
- send(data: any): boolean {
75
- if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
76
- return false;
77
- }
78
-
79
- try {
80
- const message = typeof data === 'string' ? data : JSON.stringify(data);
81
- this.socket.send(message);
82
- return true;
83
- } catch (e) {
84
- console.error('메시지 전송 오류:', e);
85
- return false;
86
- }
87
- }
88
-
89
- private attemptReconnect(): void {
90
- if (this.reconnectAttempts >= this.maxReconnectAttempts) {
91
- console.error('최대 재연결 시도 횟수 도달');
92
- return;
93
- }
94
-
95
- this.reconnectAttempts++;
96
-
97
- // 지수 백오프 적용
98
- const timeout = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
99
-
100
- this.reconnectTimeout = setTimeout(() => {
101
- console.log(`재연결 시도 중... 시도 ${this.reconnectAttempts}`);
102
- this.connect();
103
- }, timeout);
104
- }
105
-
106
- isConnected(): boolean {
107
- return this.socket !== null && this.socket.readyState === WebSocket.OPEN;
108
- }
109
-
110
- updateHandlers(handlers: SocketEventHandlers): void {
111
- this.handlers = { ...this.handlers, ...handlers };
112
- }
113
- }
114
-
115
- export default SocketManager;