mirror of
https://github.com/arthur-pbty/syncfilm.git
synced 2026-06-11 15:57:37 +02:00
Remove Madame Mode feature and associated styles, refactor video player server logic, and add MIT license and new styles for video player interface.
This commit is contained in:
@@ -1,65 +1,44 @@
|
||||
require("dotenv").config();
|
||||
const express = require("express");
|
||||
const http = require("http");
|
||||
const { Server } = require("socket.io");
|
||||
const path = require("path");
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server, {
|
||||
path: process.env.SOCKET_IO_PATH || "/socket.io", // Custom socket.io path
|
||||
});
|
||||
|
||||
// Serve static files from the public directory
|
||||
app.use(express.static(path.join(__dirname, process.env.STATIC_FILES_PATH || "public")));
|
||||
|
||||
let expectedResponses = 0; // Number of connected clients
|
||||
let receivedResponses = 0; // Number of clients that acknowledged an action
|
||||
let emitData = {}; // Data to synchronize across clients
|
||||
|
||||
// Serve the main HTML file
|
||||
app.get("/", (req, res) => {
|
||||
res.sendFile(path.join(__dirname, "index.html"));
|
||||
});
|
||||
|
||||
// Handle socket.io connections
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`User connected: ${socket.id}`);
|
||||
|
||||
// Listen for the user's name
|
||||
socket.on("setName", (name) => {
|
||||
socket.data.name = name; // Store the user's name in the socket object
|
||||
expectedResponses++;
|
||||
io.emit("notification", { message: `${name} connected`, color: "green" });
|
||||
});
|
||||
|
||||
// Handle actions (e.g., play/pause, seek)
|
||||
socket.on("action", (data) => {
|
||||
io.emit("make", data); // Broadcast the action to all clients
|
||||
emitData = data; // Save the action data
|
||||
receivedResponses = 0; // Reset the acknowledgment counter
|
||||
});
|
||||
|
||||
// Handle acknowledgment from clients
|
||||
socket.on("ok", (receivedData) => {
|
||||
if (JSON.stringify(receivedData) === JSON.stringify(emitData)) {
|
||||
receivedResponses++;
|
||||
if (receivedResponses === expectedResponses) {
|
||||
io.emit("allOk", emitData); // Notify all clients that everyone is synchronized
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Handle disconnections
|
||||
socket.on("disconnect", () => {
|
||||
const name = socket.data.name || "Unknown User";
|
||||
console.log(`User disconnected: ${name}`);
|
||||
expectedResponses--;
|
||||
io.emit("notification", { message: `${name} disconnected`, color: "red" });
|
||||
});
|
||||
});
|
||||
|
||||
// Start the server
|
||||
server.listen(process.env.PORT || 3000, () => {
|
||||
console.log(`Listening on *:${process.env.PORT || 3000}`);
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const { Server } = require('socket.io');
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server);
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.use(express.static('public'));
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/public/index.html');
|
||||
});
|
||||
|
||||
const users = {};
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('Un utilisateur est connecté');
|
||||
|
||||
users[socket.id] = { id: socket.id, filename: null };
|
||||
io.emit('users', Object.values(users));
|
||||
|
||||
socket.on('videoSelected', (filename) => {
|
||||
users[socket.id].filename = filename || null;
|
||||
io.emit('users', Object.values(users));
|
||||
});
|
||||
|
||||
// Synchronisation des commandes vidéo
|
||||
socket.on('videoCommand', (cmd) => {
|
||||
io.emit('videoCommand', cmd);
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Un utilisateur s\'est déconnecté');
|
||||
delete users[socket.id];
|
||||
io.emit('users', Object.values(users));
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`Serveur en ligne sur http://localhost:${port}`);
|
||||
});
|
||||
Reference in New Issue
Block a user