-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (40 loc) · 1.21 KB
/
index.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
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const path = require("path");
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
let connections = [];
io.on("connect", (socket) => {
connections.push(socket);
console.log(`${socket.id} has connected`);
socket.on('draw', (data) => {
connections.forEach(con => {
if(con.id !== socket.id) {
con.emit("ondraw", { x: data.x, y: data.y });
}
});
});
socket.on("down", (data) => {
connections.forEach((con) => {
if(con.id !== socket.id) {
con.emit("ondown", { x: data.x, y: data.y });
}
});
});
socket.on("disconnect", (reason) => {
console.log(`${socket.id} is disconnected`);
connections = connections.filter((con) => con.id !== socket.id);
});
});
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT || 8080;
// Export the server handler for Vercel
module.exports = (req, res) => {
server.emit('request', req, res);
};
if (!module.parent) {
server.listen(PORT, () => console.log(`Server started on port ${PORT}`));
}