亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

nodejs+websocket實(shí)時聊天系統(tǒng)改進(jìn)版

 更新時間:2017年05月18日 11:44:47   作者:蘇黎世寶貝  
這篇文章主要介紹了nodejs+websocket實(shí)時聊天系統(tǒng)的改進(jìn)版,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文屬于nodejs+websocket實(shí)時聊天系統(tǒng)的改進(jìn)版本,具體內(nèi)容如下

自己也是真的菜,一個websocket簡單聊天系統(tǒng)硬被我搞了那么些天。

看來以后還是得寫更多的代碼。

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>連接用戶:</label>
 <input type="text" id="name" />
 <button id="conn">連接</button>
 <button id="close">斷開</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 input = document.getElementById("name");
 var conn = document.getElementById("conn");
 var close = document.getElementById("close");
 var mess = document.getElementById("mess");
 var value1 = document.getElementById("value1");
 var pattern = /^[\u4e00-\u9fa5]{2,10}$/;

 close.disabled = true;
 if (window.WebSocket) {
  conn.onclick = function () {
  if (!pattern.test(input.value)) {
   alert("名稱不能為空且必須為中文");
   return;
  }
  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(input.value);
   input.setAttribute("readOnly", 'true');
   value1.setAttribute("readOnly", 'true');
  }
  ws.onmessage = function (e) {
   value1.removeAttribute("readOnly");
   var time = new Date();
   mess.innerHTML += time.toUTCString() + ":" + e.data + "<br>";
   document.getElementById("send").onclick = function (e) {
   ws.send(input.value + "說:" + value1.value);
   value1.value = " ";
   }
   document.onkeydown = function (e) {
   e = e || window.event;
   if (e.keyCode == 13) {
    document.getElementById("send").onclick();
    return false;
   }
   }
  }
  ws.onclose = function (e) {
   console.log("服務(wù)器關(guān)閉");
  }
  ws.onerror = function () {
   console.log("連接出錯");
  }
  /**
   * 客戶端主動斷開連接
   * 
   * **/
  close.onclick = function () {
   ws.onclose();
   ws.send(input.value + 'close' + "了連接");
   input.removeAttribute("readOnly");
   conn.disabled = false;
   close.disabled = true;
  }

  }


 }
 </script>
</body>

</html>

 只能說界面丑陋忽略它。
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連接完畢")

簡單的截圖說明一下:

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

就是這種呀,邏輯都錯亂了,理清邏輯是個費(fèi)腦子的事情。有知道的同學(xué)嗎?怎么處理呀

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論