nodejs+websocket實(shí)時(shí)聊天系統(tǒng)改進(jìn)版
本文屬于nodejs+websocket實(shí)時(shí)聊天系統(tǒng)的改進(jìn)版本,具體內(nèi)容如下
自己也是真的菜,一個(gè)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("連接出錯(cuò)");
}
/**
* 客戶端主動斷開連接
*
* **/
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í)行兩遍,就可以聊天啦。反正過程就是這樣。就是還有一個(gè)大的問題。比如我a斷開連接了,只有b一個(gè)人在連接巨人b還能發(fā)信息,可以發(fā)信息算可以吧。居然b自己接收自己的信息。那就不能忍了。

就是這種呀,邏輯都錯(cuò)亂了,理清邏輯是個(gè)費(fèi)腦子的事情。有知道的同學(xué)嗎?怎么處理呀
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
node.js+postman實(shí)現(xiàn)模擬HTTP服務(wù)器與客戶端交互
本文主要介紹了node.js+postman實(shí)現(xiàn)模擬HTTP服務(wù)器與客戶端交互,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
淺析node中間件及實(shí)現(xiàn)一個(gè)簡單的node中間件
這篇文章主要介紹了淺析node中間件及實(shí)現(xiàn)一個(gè)簡單的node中間件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
nodejs發(fā)布靜態(tài)https服務(wù)器步驟指南
這篇文章主要為大家介紹了nodejs發(fā)布靜態(tài)https服務(wù)器的步驟指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02
解決下載了nodejs但npm?-v沒有反應(yīng)問題的全過程
最近工作中遇到了個(gè)問題,node安裝成功,但npm無法使用,所以下面這篇文章主要給大家介紹了關(guān)于下載了nodejs但npm?-v沒有反應(yīng)問題解決的相關(guān)資料,需要的朋友可以參考下2022-08-08
Nodejs 和Session 原理及實(shí)戰(zhàn)技巧小結(jié)
這篇文章主要介紹了Nodejs 和Session 原理及實(shí)戰(zhàn)技巧小結(jié),需要的朋友可以參考下2017-08-08

