1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)

      nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng),相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

      目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、棗強(qiáng)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

      client.html:

      
      
      
      
       
       
       
       Document
       
      
      
      
       
       
       連接
       斷開

       
           發(fā)送  
       

       只能說界面丑陋忽略它。
      server.js:

      var ws = require("nodejs-websocket");
      console.log("開始建立連接...");
      var str1 = null, str2 = null, clientReady = false, serverReady = false;
      var a = [];
      var server = ws.createServer(function (conn) {
       conn.on('text', function (str) {
       a.push(str);
       if (!clientReady) {
        if (a[0] === str) {
        str1 = conn;
        clientReady = true;
        str1.sendText("歡迎你" + str);
      
        }
       } else if (!serverReady) {
        if (str.indexOf('close') >= 0) {
        a.splice(2, 1);
        clientReady = false;
        str1 = null;
        return;
        }
        if (a[1] === str) {
        str2 = conn;
        serverReady = true;
        str2.sendText("歡迎你" + str);
        str1.sendText(str + "在線啦,你們可以聊天啦");
        return;
        }
       } else if (clientReady && serverReady) {
        str2.sendText(str);
        str1.sendText(str);
        if (str.indexOf('close') >= 0) {
        a.splice(2, a.length);
        var len = a.length;
        for (var i = 0; i < len; i++) {
         // 定位該元素位置
         if (str.indexOf(a[i]) >= 0) {
         a.splice(i, 1);
         if (i == 0) {
          str1 = str2;
         }
         serverReady = false;
         str2 = null;
         return;
         }
      
        }
        }
       }
      
      
       })
      
       conn.on("close", function (code, reason) {
       console.log("關(guān)閉連接");
       clientReady = false;
       serverReady = false;
       })
       conn.on("error", function (code, reason) {
       console.log("異常關(guān)閉");
       });
      }).listen(8082);
      console.log("websocket連接完畢")

      簡(jiǎn)單的截圖說明一下:

      nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng) nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)

      nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)

      開啟服務(wù),相同頁(yè)面執(zhí)行兩遍,就可以聊天啦。反正過程就是這樣。就是還有一個(gè)大的問題。比如我a斷開連接了,只有b一個(gè)人在連接巨人b還能發(fā)信息,可以發(fā)信息算可以吧。居然b自己接收自己的信息。那就不能忍了。

      nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)

      看完上述內(nèi)容,你們掌握nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


      網(wǎng)頁(yè)標(biāo)題:nodejs中怎么利用websocket實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天系統(tǒng)
      網(wǎng)站URL:http://www.ef60e0e.cn/article/isgppc.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        华宁县| 梁平县| 三穗县| 来安县| 遂昌县| 象州县| 河北省| 弥渡县| 淮阳县| 防城港市| 平南县| 久治县| 德庆县| 盈江县| 广宁县| 兴安盟| 湖南省| 漾濞| 大同县| 海林市| 邢台市| 兴仁县| 新郑市| 龙游县| 黎平县| 吉林省| 清苑县| 丘北县| 福清市| 白朗县| 清远市| 上虞市| 阜阳市| 抚远县| 陆丰市| 武邑县| 澎湖县| 彭阳县| 喀喇| 上思县| 河池市|