-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSocketIOServer.js
More file actions
49 lines (41 loc) · 1.09 KB
/
SocketIOServer.js
File metadata and controls
49 lines (41 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { Server } = require("socket.io");
const readline = require("readline");
const io = new Server("3000", {
cors: {
origin: "*",
},
});
io.on("connection", (socket) => {
console.log(`客户端已连接: ${socket.id}`);
socket.on("message", (data) => {
console.log("收到客户端消息:", data);
console.log(data.name);
socket.emit("message", data);
});
socket.on("disconnect", () => {
console.log(`客户端断开连接: ${socket.id}`);
});
});
console.log("Socket.IO 服务启动,监听端口 3000");
// 创建命令行接口,监听输入
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '请输入服务器消息> '
});
rl.prompt();
rl.on('line', (line) => {
const msg = line.trim();
if(msg.length > 0) {
// 给所有客户端广播 message 事件
io.emit('message', msg);
if(msg === '333'){
io.emit('getAccountInfo', msg);
}
console.log(`已广播消息: ${msg}`);
}
rl.prompt();
}).on('close', () => {
console.log('服务器输入关闭,退出程序');
process.exit(0);
});