簡體   English   中英

使用node.js通過鏈接創建私人房間

[英]Creating a private room through a link using node.js

我已經讀完了:

https://socket.io/docs/rooms-and-namespaces/#

與socket.io私人聊天

我正在嘗試進行以下公共聊天:

“/”

在/ xyz上進行私人聊天,使用此URL的每個人都可以進行交談。

我將生成隨機鏈接並在以后解決它們,但是首先我需要弄清楚如何將公共用戶和私有用戶連接到不同的套接字? 特別是因為他們在做同一件事,所以我根本不知道如何有效地做到這一點。

因此,首先我必須使用以下命令捕獲服務器/專用URL:

app.get("/private",function(req,res){
res.render("page");
console.log("Rendered private page"); });

我首先想到的解決方案是使用自定義名稱空間。

    var namespace = io.of('/private');
namespace.on('connection',function(socket){
    console.log('someone connected to private');
    socket.emit('pmessage',{message:'Connected to a private chat!'});

});

但這對我的前端來說是個問題(因為我對此很陌生,所以我不知道該如何操作)。 我基本上是使用重復的代碼來處理同一件事,只是針對不同的用戶子集。

所以這:

    var socket = io.connect('127.0.0.1:8090');

我需要添加一個新的套接字,對:

    var private = io.connect('127.0.0.1:8090/private');

那我只是復制所有內容嗎? 我知道這可能不是正確的解決方案。 但我不知道該去哪里。 基本上將所有內容都設為私有而不是套接字

socket.on('message',function(data){
    //Type out the message in the htmlTextField into the htmlChatContent if message was received
    //Keep the other chats
    if(data.message){
        //From w3c:The push() method adds new items to the end of an array, and returns the new length.
        //Example: ["hi","hello"] ---push("wazzzaaap")--->["hi","hello","wazzzaaap"]
        messages.push(data);
        //put messages into the HTML code
        var html = '';
        console.log("Currently in messages" + data.message);
        console.log(data.username);
        for(var i = 0;i<messages.length ;i++){
            //Put it into a string and add a HTML defined symbol sequence for a line break
            //Add username in front of it in bold
            //FIXME: Currently only able to get messages[i] which is just the content
            if(messages[i].username==null){
                html+=messages[i].message + '<br />';
            }
            else{
                html+='<b>'+ messages[i].username + ': </b>' + messages[i].message   + '<br />';

            }

        }
        //Add the message formatted into HTML into the chat content box
        htmlChatContent.innerHTML = html;
        //When sending clear the input field also

        htmlTextField.value = "";
    }
    else{
        //This means there was an error
        //Put error text inside the users text box
        console.log("Error");
        htmlTextField.innerHTML = "There was an sending error!";
    }   
});

我希望能得到有關如何處理隨機生成的鏈接的指導,我想到的是:

創建的鏈接數據庫,刪除最后一個人離開的第二個條目。 但是,如何編程動態鏈接? 我無法對500個不同的選項進行硬編碼,對嗎?

我需要添加更多代碼以使問題變得更好嗎?

我將生成隨機鏈接並在以后解決它們,但是首先我需要弄清楚如何將公共用戶和私有用戶連接到不同的套接字?

不,您需要在客戶端和服務器之間建立一個套接字。 然后,您可以將數據僅從服務器發送到某些aockets。 Socket.io有足夠的空間,這基本上意味着您可以按組管理套接字,並且可以輕松地將數據發送到該組中的套接字。

我無法硬編碼500個不同的[套接字/鏈接],對嗎?

不,那太過分了。 只需讓客戶端/服務器生成隨機網址即可。 要使它們唯一,您只需加上時間戳並添加一個隨機數:

 const id = "" + Math.floor(Math.random() * 1000) + Date.now();

現在,如果您要在http服務器上管理/驗證客戶端,則可以使用動態url,例如:

 yourserver/private/1272271737

使用快速多數民眾贊成很容易抓住他們所有:

 app.get("/private/:id", (req, res) => {
    const { id } = req params;
    // Verify the id and return the clientside code
 });

但是實際上只有套接字服務器需要知道房間ID,因此您可以使用所謂的“ hashbang url”,它們看起來像:

 yourserver/private#127272

在服務器端,看起來客戶端是否訪問了/private因此您只需返回應用程序即可:

 app.get("/private", (req, res) => /*...*/);

但是在客戶端,您可以通過以下方式獲取ID:

 const id = location.hash;

現在,客戶可以加入相關的會議室:

socket.join(id);

現在,在發送消息時,只需發送房間ID:

 socket.emit("msg", id, "Hi!");

在服務器上,您只是將其廣播到該房間:

io.on('connection', (socket) => {
  socket.on("msg", (id, msg) => {
    io.to(id).emit("msg", msg);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM