-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.31 KB
/
server.js
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
50
51
52
/*
* @Author: SUN HENG
* @Date: 2023-09-12 10:26:29
* @LastEditors: SUN HENG && 17669477887
* @LastEditTime: 2023-11-13 10:03:31
* @FilePath: \Electronvite\server.js
* @Description:
*/
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const cors = require('cors');
const app = express();
app.use(cors({
origin: 'http://localhost:5173',
optionsSuccessStatus: 200 // 一些浏览器需要设置该项才能返回正确的状态码
}));
const server = http.createServer(app);
const io = socketIO(server, {
cors: {
origin: 'http://localhost:5173',
methods: ['GET', 'POST'],
allowedHeaders: ['my-custom-header'],
credentials: true
}
});
// 监听连接事件
io.on('connection', (socket) => {
console.log('新的 Socket.IO 连接已建立');
// 处理文件传输请求
socket.on('file', (data) => {
console.log(data,'filefilefile');
// 将文件数据广播给其他连接的客户端
socket.broadcast.emit('file', data);
});
// 处理断开连接事件
socket.on('disconnect', () => {
console.log('Socket.IO 连接已断开');
});
});
const IP_ADDRESS = '172.10.10.166';
const PORT = 3000;
// 启动服务器
server.listen(PORT,IP_ADDRESS, () => {
console.log('Socket.IO 服务器已启动,监听端口 3000');
});