iosignal 3.0.0 → 3.0.1

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.
Files changed (35) hide show
  1. package/README.md +144 -1
  2. package/dist/io.d.ts +481 -0
  3. package/dist/io.js +3 -3
  4. package/dist/io.js.map +1 -1
  5. package/dist/io.min.js +3 -3
  6. package/dist/io.min.js.map +1 -1
  7. package/dist/iosignal.js +543 -102
  8. package/dist/types/client/IOCore.d.ts +408 -0
  9. package/dist/types/client/browser/IOWebSocket.d.ts +75 -0
  10. package/dist/types/common/constants.d.ts +169 -0
  11. package/dist/types/common/payload.d.ts +6 -0
  12. package/dist/types/common/quotaTable.d.ts +47 -0
  13. package/docs/{README.kr.md → README.ko.md} +143 -2
  14. package/examples/react-chat-js/dist/assets/index-Cy4qaVcJ.js +56 -0
  15. package/examples/react-chat-js/dist/assets/index-NETrjYhz.css +1 -0
  16. package/examples/react-chat-js/dist/index.html +14 -0
  17. package/examples/react-chat-js/index.html +13 -0
  18. package/examples/react-chat-js/package-lock.json +1628 -0
  19. package/examples/react-chat-js/package.json +20 -0
  20. package/examples/react-chat-js/src/App.css +61 -0
  21. package/examples/react-chat-js/src/App.jsx +125 -0
  22. package/examples/react-chat-js/src/index.css +13 -0
  23. package/examples/react-chat-js/src/main.jsx +13 -0
  24. package/examples/react-chat-js/src/shared_io.js +12 -0
  25. package/examples/react-chat-js/vite.config.js +7 -0
  26. package/examples/server/index.js +12 -0
  27. package/examples/server/package.json +12 -0
  28. package/package.json +9 -3
  29. package/rollup.config.js +11 -4
  30. package/src/client/IOCongSocket.js +20 -9
  31. package/src/client/IOCore.js +422 -77
  32. package/src/client/IOWS.js +12 -15
  33. package/src/client/browser/IOWebSocket.js +112 -13
  34. package/src/common/constants.js +91 -1
  35. package/tsconfig.json +24 -0
@@ -1,4 +1,5 @@
1
- [ [en](../README.md) | kr ]
1
+ [ [English](../README.md) | 한국어 ]
2
+
2
3
 
3
4
  # IOSignal
4
5
 
@@ -12,6 +13,146 @@ $ npm i iosignal
12
13
 
13
14
  ## IOSignal 클라이언트
14
15
 
16
+ ### React 클라이언트 예제
17
+
18
+ `examples/react-chat-js`에서 샘플 React 프로젝트를 확인할 수 있습니다. 이 예제는 실시간 채팅 기능을 위해 `iosignal`을 React 애플리케이션에 통합하는 방법을 보여줍니다.
19
+
20
+ **주요 개념:**
21
+
22
+ - **`useRef`**: 컴포넌트가 리렌더링되어도 `io` 인스턴스에 대한 안정적인 참조를 유지합니다.
23
+ - **`useEffect`**: `io` 인스턴스의 생명주기를 관리하며, 초기화 및 정리 작업을 포함합니다. 이벤트 핸들러는 이 훅 내에서 설정됩니다.
24
+
25
+ **예제 실행 방법:**
26
+
27
+ 1. 서버 디렉토리로 이동하여 서버를 시작합니다:
28
+ ```shell
29
+ cd examples/server
30
+ npm install
31
+ node .
32
+ ```
33
+ 2. 새 터미널에서 React 프로젝트 디렉토리로 이동하여 종속성을 설치하고 개발 서버를 시작합니다:
34
+ ```shell
35
+ cd examples/react-chat-js
36
+ npm install
37
+ npm run dev
38
+ ```
39
+
40
+ **예제 코드 (`App.jsx`):**
41
+
42
+ 여러 브라우저를 열고 메시지를 입력하면 서버를 통해 서로에게 전달됩니다.
43
+
44
+ ```javascript
45
+ import { useState, useEffect, useRef } from 'react';
46
+ import './App.css';
47
+ import IO from 'iosignal/io.js';
48
+
49
+ const url = 'ws://localhost:7777';
50
+ const channel_tag = 'channel#topic';
51
+
52
+ function App() {
53
+ const [messages, setMessages] = useState([]);
54
+ const [input, setInput] = useState('Hello, World!');
55
+ const [ioState, setIoState] = useState(null);
56
+ const [cid, setCid] = useState(null);
57
+ const [counts, setCounts] = useState({ instances: 0, websockets: 0 });
58
+ const ioRef = useRef(null);
59
+ const messagesEndRef = useRef(null);
60
+
61
+ const scrollToBottom = () => {
62
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
63
+ };
64
+
65
+ useEffect(() => {
66
+ scrollToBottom();
67
+ }, [messages]);
68
+
69
+ useEffect(() => {
70
+ ioRef.current = new IO(url);
71
+ setCounts({ instances: IO.instanceCount, websockets: IO.webSocketCount });
72
+
73
+ const handleReady = () => {
74
+ console.log('ready cid:', ioRef.current.cid);
75
+ setCid(ioRef.current.cid);
76
+ ioRef.current.subscribe(channel_tag);
77
+ };
78
+
79
+ const handleChange = (state) => {
80
+ setIoState(state);
81
+ setCounts({ instances: IO.instanceCount, websockets: IO.webSocketCount });
82
+ };
83
+
84
+ const handleChannelMessage = (msgObj, tag) => {
85
+ console.log('Received message in App:', msgObj, tag);
86
+ if (typeof msgObj === 'string') {
87
+ msgObj = { text: msgObj, cid: 'cli unknown' };
88
+ }
89
+ setMessages((prevMessages) => [...prevMessages, `${msgObj.cid} : ${msgObj.text}`]);
90
+ };
91
+
92
+ const handleError = (error) => {
93
+ console.error('IO Error in App:', error);
94
+ setIoState(`Error: ${error.message}`);
95
+ };
96
+
97
+ ioRef.current.on('ready', handleReady);
98
+ ioRef.current.on('change', handleChange);
99
+ ioRef.current.on(channel_tag, handleChannelMessage);
100
+ ioRef.current.on('error', handleError);
101
+
102
+ return () => {
103
+ ioRef.current.destroy();
104
+ console.log('IO instance destroyed.');
105
+ ioRef.current = null;
106
+ };
107
+ }, []);
108
+
109
+ const sendMessage = () => {
110
+ if (input.trim()) {
111
+ const msgObj = { text: input, cid: ioRef.current.cid };
112
+ ioRef.current.signal(channel_tag, msgObj);
113
+ setInput('date' + Date.now());
114
+ }
115
+ };
116
+
117
+ const ioStateStyle = {
118
+ color: ioState === 'ready' ? 'green' : 'red',
119
+ fontWeight: 'bold',
120
+ };
121
+
122
+ return (
123
+ <div className="App">
124
+ <h1>React Chat Example</h1>
125
+ <div>URL: {url}</div>
126
+ <div>Channel: {channel_tag}</div>
127
+ <div>IO State: <span style={ioStateStyle}>{ioState}</span></div>
128
+ <div>Client ID: {cid}</div>
129
+ <div>IO Instances: {counts.instances}</div>
130
+ <div>WebSockets Created: {counts.websockets}</div>
131
+ <div className="messages">
132
+ {messages.map((msg, index) => (
133
+ <div key={index}>{msg}</div>
134
+ ))}
135
+ <div ref={messagesEndRef} />
136
+ </div>
137
+ <div className="input-area">
138
+ <input
139
+ type="text"
140
+ value={input}
141
+ onChange={(e) => setInput(e.target.value)}
142
+ onKeyUp={(e) => e.key === 'Enter' && sendMessage()}
143
+ disabled={ioState !== 'ready'}
144
+ />
145
+ <button onClick={sendMessage} disabled={ioState !== 'ready'}>
146
+ Send
147
+ </button>
148
+ </div>
149
+ </div>
150
+ );
151
+ }
152
+
153
+ export default App;
154
+ ```
155
+
15
156
  ### 브라우저 클라이언트 : ESM
16
157
 
17
158
  - 로컬 파일: "dist/io.js"
@@ -200,7 +341,7 @@ Fri, 09 Feb 2024 14:24:37 GMT
200
341
  - 아두이노는 CongSocket을 사용합니다.
201
342
 
202
343
  ## IOSignal 서버 아키텍처
203
- ![IOSignal](./iosignal_architecture.png)
344
+ ![IOSignal](../iosignal_architecture.png)
204
345
 
205
346
  ## IOSignal 저장소
206
347