-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (32 loc) · 855 Bytes
/
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
const express = require('express');
const http = require('http');
const cors = require('cors');
const { Server } = require('socket.io');
const app = express();
app.use(cors({
origin: '*'
}));
const server = http.createServer(app);
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
transports: ["websocket", "polling"],
credentials: true,
},
allowEIO3: true,
});
let brightness = 0;
io.on('connection', (socket) => {
console.log('New WebSocket connection');
socket.emit('brightness', brightness);
socket.on('brightness', (newBrightness) => {
brightness = newBrightness;
console.log('Brightness updated:', brightness);
io.emit('brightness', brightness);
});
});