Помогите решить проблему с Node.JS
Я начал изучать Node.JS и попробовал создать видеочат ! Делал по этому примеру в YouTube-https://youtu.be/InK2Zkobgp8 Проект запустился, но у меня не срабатывают кнопки и не работает чат, но интерфейс я вижу.
-----------------------------SERVER.JS
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.redirect(`/${uuidv4()}`);
});
app.get('/:room', (req, res) => {
res.render('room', { roomId: req.params.room });
});
const server = http.createServer(app);
const io = new Server(server);
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
----------------------------CSS
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100%;
font-family: "Roboto", sans-serif;
}
#video-grid {
display: flex;
justify-content: center;
height: 100%;
width: 100%;
align-items: center;
flex-wrap: wrap;
overflow-y: auto;
}
video {
display: block;
flex: 1;
object-fit: cover;
border: 5px solid #000;
max-width: 600px;
}
.main {
height: 100%;
display: flex;
}
.main__left {
flex: 0.8;
display: flex;
flex-direction: column;
}
.main__right {
flex: 0.2;
display: flex;
flex-direction: column;
background-color: #242324;
border-left: 1px solid #3d3d42;
}
.main__videos {
flex-grow: 1;
background-color: #000;
display: flex;
align-items: center;
justify-content: center;
}
.main__controls {
display: flex;
background-color: #1c1e20;
color: #d2d2d2;
padding: 5px;
justify-content: space-between;
}
.main__controls_block {
display: flex;
}
.main__controls_button {
display: flex;
flex-direction: column;
cursor: pointer;
padding: 10px;
justify-content: center;
align-items: center;
min-width: 80px;
transition: all 0.3s ease-in-out;
border-radius: 10px;
margin: 5px;
}
.main__controls_button span {
margin-top: 10px;
display: block;
}
.main__controls_button:hover {
background-color: #34383b;
}
.main__controls_button i {
font-size: 25px;
}
.leaveMeeting {
background-color: red;
color: #fff;
}
.main__header {
color: #f5f5f5;
text-align: center;
padding: 20px;
border-bottom: 2px solid #3d3d42;
}
.main__chat__window {
flex-grow: 1;
overflow: auto;
padding: 20px 20px 0 20px;
}
.main__message_container {
padding: 22px 11px;
display: flex;
}
.main__message_container input {
flex-grow: 1;
background-color: transparent;
border: none;
color: #f5f5f5;
user-select: none;
outline: none;
}
#all_messages li {
color: #fff;
list-style: none;
border-bottom: 1px solid #3d3d42;
padding: 10px 0;
}
.unmute {
color: red;
}
----------------JS SCRIPT
const socket = io("/");
const chatInputBox = document.getElementById("chat_message");
const all_messages = document.getElementById("all_messages");
const main__chat__window = document.getElementById("main__chat__window");
const videoGrid = document.getElementById("video-grid");
const myVideo = document.createElement("video");
myVideo.muted = true;
var peer = new Peer(undefined, {
path: "/peerjs",
host: "/",
port: "3000",
});
let myVideoStream;
var getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
navigator.mediaDevices
.getUserMedia({
video: true,
audio: true,
})
.then((stream) => {
myVideoStream = stream;
addVideoStream(myVideo, stream);
peer.on("call", (call) => {
call.answer(stream);
const video = document.createElement("video");
call.on("stream", (userVideoStream) => {
addVideoStream(video, userVideoStream);
});
});
socket.on("user-connected", (userId) => {
connectToNewUser(userId, stream);
});
document.addEventListener("keydown", (e) => {
if (e.which === 13 && chatInputBox.value != "") {
socket.emit("message", chatInputBox.value);
chatInputBox.value = "";
}
});
socket.on("createMessage", (msg) => {
console.log(msg);
let li = document.createElement("li");
li.innerHTML = msg;
all_messages.append(li);
main__chat__window.scrollTop = main__chat__window.scrollHeight;
});
});
peer.on("call", function (call) {
getUserMedia(
{ video: true, audio: true },
function (stream) {
call.answer(stream); // Answer the call with an A/V stream.
const video = document.createElement("video");
call.on("stream", function (remoteStream) {
addVideoStream(video, remoteStream);
});
},
function (err) {
console.log("Failed to get local stream", err);
}
);
});
peer.on("open", (id) => {
socket.emit("join-room", ROOM_ID, id);
});
// CHAT
const connectToNewUser = (userId, streams) => {
var call = peer.call(userId, streams);
console.log(call);
var video = document.createElement("video");
call.on("stream", (userVideoStream) => {
console.log(userVideoStream);
addVideoStream(video, userVideoStream);
});
};
const addVideoStream = (videoEl, stream) => {
videoEl.srcObject = stream;
videoEl.addEventListener("loadedmetadata", () => {
videoEl.play();
});
videoGrid.append(videoEl);
let totalUsers = document.getElementsByTagName("video").length;
if (totalUsers > 1) {
for (let index = 0; index < totalUsers; index++) {
document.getElementsByTagName("video")[index].style.width =
100 / totalUsers + "%";
}
}
};
const playStop = () => {
let enabled = myVideoStream.getVideoTracks()[0].enabled;
if (enabled) {
myVideoStream.getVideoTracks()[0].enabled = false;
setPlayVideo();
} else {
setStopVideo();
myVideoStream.getVideoTracks()[0].enabled = true;
}
};
const muteUnmute = () => {
const enabled = myVideoStream.getAudioTracks()[0].enabled;
if (enabled) {
myVideoStream.getAudioTracks()[0].enabled = false;
setUnmuteButton();
} else {
setMuteButton();
myVideoStream.getAudioTracks()[0].enabled = true;
}
};
const setPlayVideo = () => {
const html = `<i class="unmute fa fa-pause-circle"></i>
<span class="unmute">Resume Video</span>`;
document.getElementById("playPauseVideo").innerHTML = html;
};
const setStopVideo = () => {
const html = `<i class=" fa fa-video-camera"></i>
<span class="">Pause Video</span>`;
document.getElementById("playPauseVideo").innerHTML = html;
};
const setUnmuteButton = () => {
const html = `<i class="unmute fa fa-microphone-slash"></i>
<span class="unmute">Unmute</span>`;
document.getElementById("muteButton").innerHTML = html;
};
const setMuteButton = () => {
const html = `<i class="fa fa-microphone"></i>
<span>Mute</span>`;
document.getElementById("muteButton").innerHTML = html;
};
---------------------------HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HOME</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
/>
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/1.3.1/peerjs.min.js.map"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
const ROOM_ID = "<%= roomId %>";
</script>
</head>
<body>
<div class="main">
<div class="main__left">
<div class="main__videos">
<div id="video-grid"></div>
</div>
<div class="main__controls">
<div class="main__controls_block">
<div
class="main__controls_button"
id="muteButton"
onclick="muteUnmute()"
>
<i class="fa fa-microphone"></i>
<span>Mute</span>
</div>
<div
class="main__controls_button"
id="playPauseVideo"
onclick="playStop()"
>
<i class="fa fa-video-camera"></i>
<span>Pause Video</span>
</div>
</div>
<div class="main__controls_block">
<div class="main__controls_button">
<i class="fa fa-shield"></i>
<span>Security</span>
</div>
<div class="main__controls_button">
<i class="fa fa-users"></i>
<span>Participants</span>
</div>
<div class="main__controls_button">
<i class="fa fa-comment"></i>
<span>Chat</span>
</div>
</div>
<div class="main__controls_block">
<div class="main__controls_button leaveMeeting" id="leave-meeting">
<i class="fa fa-times"></i>
<span class="">Leave Meeting</span>
</div>
</div>
</div>
</div>
<div class="main__right">
<div class="main__header">
<h6>Chat</h6>
</div>
<div class="main__chat__window" id="main__chat__window">
<ul class="messages" id="all_messages"></ul>
</div>
<div class="main__message_container">
<input
type="text"
id="chat_message"
placeholder="Type message here.."
/>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>