Websocket 失败:帧头无效

发布于 2025-01-20 06:41:48 字数 8420 浏览 0 评论 0原文

I'm using socket.io with peerjs to create a video conference app it shows me this error in the console of browser :

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=O0Hrv -4& sid = 2C_B3ACCFINUUUFNAAAAA 400(不良请求) 和 WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=3&transport=websocket&sid=TIij4-ksOm6WI8URAAAA' failed: Invalid frame header

Can anyone please help? 我很快就会结束 #server.js#

const express = require('express');
const app = express();
const server = require('http').Server(app);
const { v4: uuidv4 } = require('uuid');
const io = require('socket.io')(server);
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
    debug: true
});
app.use(express.static('public'));
app.use('/peerjs', peerServer);
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
    res.redirect(`/${uuidv4()}`);
});
app.get('/:room', (req, res) => {
    res.render('room', { roomId: req.param.room })
});
io.on('connection', socket => {
    socket.on('join-room', (roomId, userId) => {
        socket.join(roomId);
        socket.to(roomId).broadcast.emit('user-connected', userId);
        socket.on('message', message => {
            io.to(roomId).emit('createMessage', message)
        })
    })
})
server.listen(process.env.PORT || 3000)

##room.ejs##
 !DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zoom-Clone</title>
    <script src="/socket.io/socket.io.js"></script>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
    <script src="https://kit.fontawesome.com/c939d0e917.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.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 onclick="muteUnmute()" class="main_controls_button main_mute_button">
                        <i class="fas fa-microphone"></i>
                        <span>Mute</span>
                    </div>
                    <div onclick="playStop()" class="main_controls_button main_video_button">
                        <i class="fas fa-video"></i>
                        <span>Stop Video</span>
                    </div>
                </div>

                <div class="main_controls_block">
                    <div class="main_controls_button">
                        <i class="fas fa-shield-alt"></i>
                        <span>Security</span>
                    </div>
                    <div class="main_controls_button">
                        <i class="fas fa-user-friends"><              <span>Participants</span>
                    </div>
                    <div class="main_controls_button">
                        <i class="fas fa-comment-alt"></i>
                        <span>Chat</span>
                    </div>
                </div>
                <div class="main_controls_block">
                    <div class="main_controls_button">
                        <span class="leave_meeting">Leave Meeting</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="main_right">
            <div class="main_header">
                <h6>Chat</h6>
            </div>
            <div class="main_chat_window">
                <ul class="messages">

                </ul>
            </div>
            <div class="main_message_container">
                <input id="chat_message" type="text" placeholder="Type message here...">
            </div>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html> 
###script.js###
 
const socket = io('/');
const videoGrid = document.getElementById('video-grid');
const myVideo = document.createElement('video');
myVideo.muted = true;
var peer = new Peer(undefined, {
    path: '/peerjs',
    host: '/',
    port: '3001'
});
let myVideoStream;
const addVideoStream = (video, stream) => {
    video.srcObject = stream;
    video.addEventListener('loadedmetadata', () => {
        video.play();
    })
    videoGrid.append(video);
};
const connectToNewUser = (userId, stream) => {
    const call = peer.call(userId, stream);
    const video = document.createElement('video');
    call.on('stream', userVideoStream => {
        addVideoStream(video, userVideoStream);
    })
}
const scrollToBottom = () => {
    let d = $('main_chat_window');
    d.scrollTop(d.prop("scrollHeight"));
}
const setMuteButton = () => {
    const html = `
    <i class="fas fa-microphone"></i>
    <span>Mute</span>`
    $('.main_mute_button').html(html);
}
const setUnmuteButton = () => {
    const html = `
    <i class="unmute fas fa-microphone-slash"></i>
    <span>Ummte</span>`
    $('.main_mute_button').html(html);
}
const muteUnmute = () => {
    const enabled = myVideoStream.getAudioTracks()[0].enabled;
    if (enabled) {
        myVideoStream.getAudioTracks()[0].enabled = false;
        setUnmuteButton();
    }
    else {
        setMuteButton();
        myVideoStream.getAudioTracks()[0].enabled = true;
    }
}
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);
    })
})
peer.on('open', id => {
    socket.emit('join-room', ROOM_ID, id);
});
let text = $('input');
$('html').keydown(e => {
    if (e.which == 13 && text.val().length !== 0) {
        socket.emit('message', text.val());
        console.log(text.val());
        text.val('')
    }
});
socket.on('createMessage', message => {
    $('ul').append(`<li class="message"><b>user</b><br />${message}</li>`)
    scrollToBottom();
})
const setPlayVideo = () => {
    const html = `
    <i class="stop fas fa-video-slash"></i>
    <span>PlayVideo</span>`
    $('.main_video_button').html(html);
}
const setStopVideo = () => {
    const html = `
    <i class="fas fa-video"></i>
    <span>Stop Video</span>`
    $('.main_video_button').html(html);
}
const playStop = () => {
    let enabled = myVideoStream.getVideoTracks()[0].enabled;
    if (enabled) {
        myVideoStream.getVideoTracks()[0].enabled = false;
        setPlayVideo();
    }
    else {
        setStopVideo();
        myVideoStream.getVideoTracks()[0].enabled = true;
    }
} 

I'm using socket.io with peerjs to create a video conference app it shows me this error in the console of browser :

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=O0Hrv-4&sid=2C_b3aCCfINuuUFNAAAA 400 (Bad Request)
and
WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=3&transport=websocket&sid=TIij4-ksOm6WI8URAAAA' failed: Invalid frame header

Can anyone please help?
i have a final soon
#server.js#

const express = require('express');
const app = express();
const server = require('http').Server(app);
const { v4: uuidv4 } = require('uuid');
const io = require('socket.io')(server);
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
    debug: true
});
app.use(express.static('public'));
app.use('/peerjs', peerServer);
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
    res.redirect(`/${uuidv4()}`);
});
app.get('/:room', (req, res) => {
    res.render('room', { roomId: req.param.room })
});
io.on('connection', socket => {
    socket.on('join-room', (roomId, userId) => {
        socket.join(roomId);
        socket.to(roomId).broadcast.emit('user-connected', userId);
        socket.on('message', message => {
            io.to(roomId).emit('createMessage', message)
        })
    })
})
server.listen(process.env.PORT || 3000)

##room.ejs##
 !DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zoom-Clone</title>
    <script src="/socket.io/socket.io.js"></script>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
    <script src="https://kit.fontawesome.com/c939d0e917.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.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 onclick="muteUnmute()" class="main_controls_button main_mute_button">
                        <i class="fas fa-microphone"></i>
                        <span>Mute</span>
                    </div>
                    <div onclick="playStop()" class="main_controls_button main_video_button">
                        <i class="fas fa-video"></i>
                        <span>Stop Video</span>
                    </div>
                </div>

                <div class="main_controls_block">
                    <div class="main_controls_button">
                        <i class="fas fa-shield-alt"></i>
                        <span>Security</span>
                    </div>
                    <div class="main_controls_button">
                        <i class="fas fa-user-friends"><              <span>Participants</span>
                    </div>
                    <div class="main_controls_button">
                        <i class="fas fa-comment-alt"></i>
                        <span>Chat</span>
                    </div>
                </div>
                <div class="main_controls_block">
                    <div class="main_controls_button">
                        <span class="leave_meeting">Leave Meeting</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="main_right">
            <div class="main_header">
                <h6>Chat</h6>
            </div>
            <div class="main_chat_window">
                <ul class="messages">

                </ul>
            </div>
            <div class="main_message_container">
                <input id="chat_message" type="text" placeholder="Type message here...">
            </div>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html> 
###script.js###
 
const socket = io('/');
const videoGrid = document.getElementById('video-grid');
const myVideo = document.createElement('video');
myVideo.muted = true;
var peer = new Peer(undefined, {
    path: '/peerjs',
    host: '/',
    port: '3001'
});
let myVideoStream;
const addVideoStream = (video, stream) => {
    video.srcObject = stream;
    video.addEventListener('loadedmetadata', () => {
        video.play();
    })
    videoGrid.append(video);
};
const connectToNewUser = (userId, stream) => {
    const call = peer.call(userId, stream);
    const video = document.createElement('video');
    call.on('stream', userVideoStream => {
        addVideoStream(video, userVideoStream);
    })
}
const scrollToBottom = () => {
    let d = $('main_chat_window');
    d.scrollTop(d.prop("scrollHeight"));
}
const setMuteButton = () => {
    const html = `
    <i class="fas fa-microphone"></i>
    <span>Mute</span>`
    $('.main_mute_button').html(html);
}
const setUnmuteButton = () => {
    const html = `
    <i class="unmute fas fa-microphone-slash"></i>
    <span>Ummte</span>`
    $('.main_mute_button').html(html);
}
const muteUnmute = () => {
    const enabled = myVideoStream.getAudioTracks()[0].enabled;
    if (enabled) {
        myVideoStream.getAudioTracks()[0].enabled = false;
        setUnmuteButton();
    }
    else {
        setMuteButton();
        myVideoStream.getAudioTracks()[0].enabled = true;
    }
}
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);
    })
})
peer.on('open', id => {
    socket.emit('join-room', ROOM_ID, id);
});
let text = $('input');
$('html').keydown(e => {
    if (e.which == 13 && text.val().length !== 0) {
        socket.emit('message', text.val());
        console.log(text.val());
        text.val('')
    }
});
socket.on('createMessage', message => {
    $('ul').append(`<li class="message"><b>user</b><br />${message}</li>`)
    scrollToBottom();
})
const setPlayVideo = () => {
    const html = `
    <i class="stop fas fa-video-slash"></i>
    <span>PlayVideo</span>`
    $('.main_video_button').html(html);
}
const setStopVideo = () => {
    const html = `
    <i class="fas fa-video"></i>
    <span>Stop Video</span>`
    $('.main_video_button').html(html);
}
const playStop = () => {
    let enabled = myVideoStream.getVideoTracks()[0].enabled;
    if (enabled) {
        myVideoStream.getVideoTracks()[0].enabled = false;
        setPlayVideo();
    }
    else {
        setStopVideo();
        myVideoStream.getVideoTracks()[0].enabled = true;
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文