通过动态URL链接的NodeJS套接字视频聊天室仅可容纳2人

如何制作执行以下操作的套接字视频聊天应用程序:

-Server.js有一个名为ids的数组

-app.get中的通配符,用于“获取”用户的url,并在主机名(hostname / *)之后切路径

-如果切片的网址在id中不存在,则生成一个随机字符串,将其推入id数组

-创建一个新的视频聊天室并将该用户发送到那里

-这个新的视频聊天室只能由一个具有url(主机名/ id)中的id的用户连接

-当两个用户都断开连接时,标识将从阵列中删除,聊天室也将被删除

我尝试使用fs生成新的html文件(通过复制和重命名),但是无法将用户重定向到新文件。我只能重定向到我自己的域或其他人的域中的现有页面,而不能重定向到新生成的html文件。尝试执行此操作时出现“内部服务器错误”。一旦两者都断开连接,也许删除新文件? Fs在生成html文件时会尽力而为,但是创建后我无法立即重定向到它。

(是的,有ejs,但是我希望允许用户使用url进行连接。这些URL必须是随机的。)

这里是密码。

const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const port = process.env.PORT || 3000
const nanoid = require('nanoid')
const path = require('path');
const fs = require('fs-extra');
var randomID = nanoid();
var ids= [];

app.use("/styles",express.static(__dirname + '/public/css'));
app.use("/scripts",express.static(__dirname + '/public/js'));
//otherwise,css and client js will not run

app.get('/*',function(req,res){
  var origin = req.url.slice(-21);
  if(ids.includes(origin)===false){
    ids.push(randomID);
    fs.copySync('public/index.html',"public/"+randomID+".html");
    //It works if I redirect to internal and external existing pages
    //var destination = '<script>window.location.href=' + '"' +"https://www.google.com.my/imghp?hl=en&tab=wi&ogbl"+ '";</script>';
    //var destination = '<script>window.location.href=' + '"' +"/-cVHe8n64LGFxOzh9YNcH.html"+ '";</script>';
    var destination = '<script>window.location.href=' + '"' +"/"+ randomId+'.html";</script>';
    //but the server says internal server error
    console.log(destination);
    console.log(urls);
    res.send(destination);
  }
});

let clients = 0
//Only two devices can be in the same page at the same time
io.on('connection',function (socket) {
    socket.on("NewClient",function () {
        if (clients < 2) {
            if (clients == 1) {
                this.emit('CreatePeer');
            }
        }
        else
            this.emit('Sessionactive')
        clients++;
    })
    socket.on('Offer',SendOffer)
    socket.on('Answer',SendAnswer)
    socket.on('disconnect',Disconnect)
})

function Disconnect() {
    if (clients > 0) {
        if (clients <= 2)
            this.broadcast.emit("Disconnect");
        clients--
    }
    else {
      //delete the file once both are gone
      urls.splice(urls.indexOf(randomID),1 );
      console.log(urls);
      fs.removeSync('public/index.html',"public/"+randomID+".html");
    }

}

function SendOffer(offer) {
    this.broadcast.emit("BackOffer",offer)
}

function SendAnswer(data) {
    this.broadcast.emit("BackAnswer",data)
}

http.listen(port,() => console.log(`active on ${port} port`))

我很迷路。

qq090630 回答:通过动态URL链接的NodeJS套接字视频聊天室仅可容纳2人

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3141924.html

大家都在问