xx-chat 1.0.3 → 1.0.4

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/README.md CHANGED
@@ -30,15 +30,16 @@ yarn global add xx-chat
30
30
  Start a chat server on your machine and join the chat room:
31
31
 
32
32
  ```bash
33
- xx-chat --role server --nickname <YourNickname>
33
+ xx-chat --role server --nickname <YourNickname> --port 3000
34
34
  ```
35
+ *(The `--port` parameter is optional and defaults to `3000`)*
35
36
 
36
37
  ### Start as a Client (Guest)
37
38
 
38
- Join an existing chat server using its IP address:
39
+ Join an existing chat server using its IP address and port:
39
40
 
40
41
  ```bash
41
- xx-chat --role client --ip <Server_IP> --nickname <YourNickname>
42
+ xx-chat --role client --ip <Server_IP> --nickname <YourNickname> --port 3000
42
43
  ```
43
44
 
44
45
  *Tip: If you run `xx-chat` without any arguments, it will launch an interactive prompt to help you configure these settings.*
@@ -86,15 +87,16 @@ yarn global add xx-chat
86
87
  在你的电脑上启动聊天服务,并作为第一个用户加入:
87
88
 
88
89
  ```bash
89
- xx-chat --role server --nickname <你的昵称>
90
+ xx-chat --role server --nickname <你的昵称> --port 3000
90
91
  ```
92
+ *(`--port` 参数是可选的,默认端口为 `3000`)*
91
93
 
92
94
  ### 作为客户端(访客)加入
93
95
 
94
- 使用服务端的 IP 地址连接到现有的聊天室:
96
+ 使用服务端的 IP 地址和端口连接到现有的聊天室:
95
97
 
96
98
  ```bash
97
- xx-chat --role client --ip <服务器的局域网IP> --nickname <你的昵称>
99
+ xx-chat --role client --ip <服务器的局域网IP> --nickname <你的昵称> --port 3000
98
100
  ```
99
101
 
100
102
  *提示:如果你直接输入 `xx-chat` 不带任何参数,程序会通过交互式的问答引导你完成这些配置。*
package/index.js CHANGED
@@ -5,19 +5,19 @@ import { startClient } from './src/client.js';
5
5
  import { initUI, renderSystemMessage, renderChatMessage, setSendColor, setRecvColor } from './src/ui.js';
6
6
 
7
7
  async function main() {
8
- const { role, ip, nickname } = await parseCli();
8
+ const { role, ip, nickname, port } = await parseCli();
9
9
 
10
10
  let socket;
11
11
 
12
12
  if (role === 'server') {
13
13
  renderSystemMessage('正在启动服务器...');
14
- startServer(3000);
15
- renderSystemMessage('服务器已启动,监听端口 3000');
14
+ startServer(port);
15
+ renderSystemMessage(`服务器已启动,监听端口 ${port}`);
16
16
  // 服务端自己也作为一个客户端连接上去
17
- socket = startClient('127.0.0.1', nickname);
17
+ socket = startClient('127.0.0.1', nickname, port);
18
18
  } else if (role === 'client') {
19
- renderSystemMessage(`正在连接到服务器 ${ip}:3000...`);
20
- socket = startClient(ip, nickname);
19
+ renderSystemMessage(`正在连接到服务器 ${ip}:${port}...`);
20
+ socket = startClient(ip, nickname, port);
21
21
  }
22
22
 
23
23
  let currentNickname = nickname;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xx-chat",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "A simple LAN CLI chat tool",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/cli.js CHANGED
@@ -5,7 +5,8 @@ export async function parseCli() {
5
5
  program
6
6
  .option('-r, --role <type>', 'Role: server or client')
7
7
  .option('-i, --ip <ip>', 'Server IP to connect to (if client)')
8
- .option('-n, --nickname <name>', 'Your nickname');
8
+ .option('-n, --nickname <name>', 'Your nickname')
9
+ .option('-p, --port <port>', 'Port number', '3000');
9
10
 
10
11
  program.parse(process.argv);
11
12
  const options = program.opts();
@@ -51,5 +52,6 @@ export async function parseCli() {
51
52
  role: answers.role || options.role,
52
53
  ip: answers.ip || options.ip,
53
54
  nickname: answers.nickname || options.nickname,
55
+ port: options.port,
54
56
  };
55
57
  }
package/src/client.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { io } from 'socket.io-client';
2
2
  import { renderSystemMessage, renderChatMessage } from './ui.js';
3
3
 
4
- export function startClient(ip, nickname) {
5
- const socket = io(`http://${ip}:3000`);
4
+ export function startClient(ip, nickname, port = 3000) {
5
+ const socket = io(`http://${ip}:${port}`);
6
6
 
7
7
  socket.on('connect', () => {
8
8
  socket.emit('join', nickname);