溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

websocket+node.js如何實(shí)現(xiàn)實(shí)時(shí)聊天系統(tǒng)問(wèn)題咨詢(xún)

發(fā)布時(shí)間:2021-06-21 11:25:23 來(lái)源:億速云 閱讀:155 作者:小新 欄目:web開(kāi)發(fā)

小編給大家分享一下websocket+node.js如何實(shí)現(xiàn)實(shí)時(shí)聊天系統(tǒng)問(wèn)題咨詢(xún),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.最近新學(xué)習(xí)websocket。做了一個(gè)實(shí)時(shí)聊天。用Node.js搭建的服務(wù):serevr.js. 兩個(gè)相互通信頁(yè)面:client.html 和server.html

但是就是有很多問(wèn)題,想讓知道的人幫我看看哈:

我先把代碼貼出來(lái):

server.js:

var ws=require("nodejs-websocket");
 console.log("開(kāi)始建立連接...");
 var str1=null,str2=null, clientReady=false,serverReady=false;
 var server=ws.createServer(function(conn){
   conn.on('text',function(str){
     console.log(str);
     /**
      * 用戶(hù)小雨第一次連接
      */
    if(str==="小雨"){
       str1=conn;
       clientReady=true;
       str1.sendText("歡迎"+str); 
    }
    /**
     * 用戶(hù)小喬第一次連接
     */
    if(str==="小喬"){
       str2=conn;
       serverReady=true;
      str2.sendText("歡迎"+str);
    }
    /**
     * 當(dāng)有第二個(gè)用戶(hù)連接時(shí)。
     */
     if(clientReady&&serverReady){
      str2.sendText(str);
      str1.sendText(str);
    }
   })
   conn.on("close",function(code,reason){
     console.log("關(guān)閉連接");
   })
   conn.on("error",function(code,reason){
     console.log("異常關(guān)閉")
   });
 }).listen(8082);
 console.log("websocket連接完畢")
client.html:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .kuang{
      width: 600px;
      min-height: 50px;
      max-height:296px;
      border:1px solid;
      float: left;
      display: block;
      position:relative;
      overflow-y: scroll;
    }
    .value{ 
      width: 200px;
    }
    .input{
      display:block;
      position: absolute;
      left:0;
      margin-top: 300px;
    }
  </style>
</head>
<body>
  <label>連接用戶(hù):</label>
  <input type="text" id="name" value="小雨" readonly/>
  <button id="conn">連接</button>
  <button id="close">斷開(kāi)</button><br/><br/>
  <div class="kuang" id="mess"></div>
  <div class="input"> 
  <input type="text" class="value" id="value1" />
  <button id="send">發(fā)送</button>
  </div>
  <script>
    var name=document.getElementById("name").value;
    var mess=document.getElementById("mess");
    var value1=document.getElementById("value1");
    var conn= document.getElementById("conn");
    var close=document.getElementById("close");
    close.disabled=true;
    if(window.WebSocket){
     conn.onclick=function(){
      var ws=new WebSocket('ws://127.0.0.1:8082');
      conn.disabled=true;
      close.disabled=false; 
       ws.onopen=function(e){
          console.log("連接服務(wù)器成功"); 
           ws.send(name);
      } 
    ws.onmessage=function(e){
      var time=new Date();
      mess.innerHTML+=time.toUTCString()+":"+e.data+"<br>";
      document.getElementById("send").onclick=function(e){
        ws.send(name+"說(shuō):"+value1.value);
        value1.value=" ";
      }
      document.onkeydown = function(e) {
        e = e || window.event;
        if(e.keyCode == 13) {
           document.getElementById("send").onclick();
          return false;
        }
      }  
    }
    /**
     * 客戶(hù)端主動(dòng)斷開(kāi)連接
     * 
     * **/ 
    close.onclick=function(){
      ws.onclose();
      conn.disabled=false;
      close.disabled=true; 
    }  
     ws.onclose = function(e){
       console.log("服務(wù)器關(guān)閉");
     } 
    ws.onerror = function(){
      console.log("連接出錯(cuò)");
    }
    }  
  }
  </script>
</body>
</html>

server.html 頁(yè)面和client.html的代碼一樣,就是用戶(hù)名字換成小喬啦。

接下來(lái)就是問(wèn)題啦:

1.運(yùn)行界面:

  client.html  連接以后:

websocket+node.js如何實(shí)現(xiàn)實(shí)時(shí)聊天系統(tǒng)問(wèn)題咨詢(xún)

本來(lái)服務(wù)器只需要回傳一個(gè)歡迎小雨的,然后下面還輸出了一個(gè)。

server.html  小喬連接以后也出來(lái)了一個(gè)小喬,按理是歡迎小喬。然后告訴小喬小雨在線了。

websocket+node.js如何實(shí)現(xiàn)實(shí)時(shí)聊天系統(tǒng)問(wèn)題咨詢(xún)

2.兩個(gè)頁(yè)面代碼一樣,但是就是不能只變成一個(gè)頁(yè)面,硬要兩個(gè)才能聊天。

3.server.js那邊邏輯有點(diǎn)問(wèn)題,一直理不清楚。

以上是“websocket+node.js如何實(shí)現(xiàn)實(shí)時(shí)聊天系統(tǒng)問(wèn)題咨詢(xún)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI