这是目前我结合网上的资料整理出的一份最简洁的webRTC的实践案例。使用前需先安装好node和npm,之后用npm安装express, express-ws, cors库
由于没有使用https和wss协议,只能在自己的电脑上使用。在内网中会发现打不开摄像头,之后websocket也建立不成功。
信令服务器:(文件名为server.js)
const app = require('express')();
const wsInstance = require('express-ws')(app);
const cors = require('cors');
app.use(cors({ origin: 'http://localhost:3000' }));
app.ws('/', ws => {
ws.on('message', data => {
// 未做业务处理,收到消息后直接广播
wsInstance.getWss().clients.forEach(server => {
if (server !== ws) {
server.send(data);
console.log(data,)
}
});
});
});
app.listen(8080, '0.0.0.0');
发送端:(send.html)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=ed