多人在线聊天
![在这里插入图片描述](https://img-blog.csdnimg.cn/95362ad573cc4d2083fbdac2c86dbbcd.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBATHV0cnJh,size_20,color_FFFFFF,t_70,g_se,x_16)
步骤
准备工作
- 在控制台输入 npm init
- npm install express
- npm install socket.io
html主页
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>chat_message</title>
<style>
*{
padding: 0;
margin: 0;
}
.chat_commento{
width: 450px;
height: 650px;
margin: auto;
border-radius: 10px;
border: 2px solid #f4f5f7;
}
.clearfix::after{
content: "";
display: block;
clear: both;
width: 0;
height: 0;
line-height: 0;
visibility: hidden;
}
.chat_top{
width: 100%;
height: 50px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
padding-left: 20px;
font-size: 20px;
line-height: 50px;
box-sizing: border-box;
font-weight: 550;
border-width: 0px;
}
.chat_middle{
width: 450px;
height: 455px;
position: relative;
box-sizing: border-box;
overflow: auto;
border-width: 0px;
}
.chat_left{
width: 100%;
height: 120px;
margin-top: 20px;
}
.chat_left_item_1{
width: 50px;
height: 50px;
background-color: #4459AB;
float: left;
margin-top: 10px;
margin-left: 10px;
margin-right: 10px;
text-align: center;
line-height: 50px;
color: white;
border-radius: 25px;
}
.chat_left_item_2{
width: 55%;
height: 100px;
float: left;
margin-top: 10px;
}
.chat_left_item_2 .chat_left_chat{
float: left;
}
.chat_left_item_2 .chat_left_content{
padding: 15px;
margin-top: 10px;
background-color: #f4f5f7;
display: inline-block;
border-radius: 10px;
border-top-left-radius: 0px;
word-break:break-all;
}
.chat_right{
width: 100%;
height: 120px;
margin-top: 20px;
}
.chat_right_item_1{
width: 50px;
height: 50px;
background-color: #4459AB;
float: right;
margin-top: 10px;
margin-left: 10px;
margin-right: 10px;
text-align: center;
line-height: 50px;
color: white;
border-radius: 25px;
display: inline-block;
}
.chat_right_item_2{
width: 55%;
height: 100px;
float: right;
margin-top: 10px;
}
.chat_right_time{
width: 100%;
text-align: right;
}
.chat_right_content{
float: right;
padding: 15px;
border-radius: 10px;
margin-top: 10px;
border-top-right-radius: 0px;
background-color: #4F7cff;
color: white;
word-break:break-all;
}
.chat_foot{
width: 450px;
height: 130px;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
position: relative;
}
.chat_context{
width: 100%;
height: 100%;
font-size: 17px;
box-sizing: border-box;
outline: none;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
border-width: 0px;
padding: 16px;
word-break:break-all;
}
.chat_commit{
width: 80px;
height: 30px;
color: white;
background-color: #4F7cff;
line-height: 30px;
text-align: center;
border-radius: 5px;
position: absolute;
right: 10px;
bottom: 20px;
margin-right: 10px;
}
.chat_context{
resize: none;
word-break:break-all;
}
.chat_context::placeholder{
color: black;
font-weight: 500k;
}
.line{
width: 100%;
border-top: 1px;
border-color: #f4f5f7;
border-style: solid;
}
</style>
</head>
<body>
<div class="chat_commento" id="chat_comment">
<div class="chat_top">
是兄弟就来砍我
</div>
<div class="line"></div>
<div class="chat_middle" id="chat_middle_item" ref="chat_middle_item">
</div>
<div class="line"></div>
<div class="chat_foot">
<textarea class="chat_context" id="chat_context_item" v-model="chat_context_item" cols="30" rows="10" placeholder="请输入"></textarea>
<div class="chat_commit" id="button" ref="button">发送</div>
</div>
</div>
<script src="/node_modules/socket.io/client-dist/socket.io.js"></script>
<script>
var names=prompt('请输入你的名字');
var socket=io()
socket.emit('user',names)
socket.on('username',function(name){
var chat_middle_item=document.getElementById("chat_middle_item");
var pro=`<div style="color: #4459AB; text-align: center; font-size: 17px; opacity: 0.5;"> ${name} 已加入群聊</div>`;
chat_middle_item.innerHTML=chat_middle_item.innerHTML+pro;
let ele = document.getElementById('chat_middle_item');
ele.scrollTop = ele.scrollHeight;
})
socket.on('message',function(data){
if(data.uesr==names){
var ans=`<div class="chat_right_item_1 clearfix">${data.uesr}</div>
<div class="chat_right_item_2">
<div class="chat_right_time clearfix">${data.times}</div>
<div class="chat_right_content clearfix">${data.text}</div>
</div>`;
var oLi=document.createElement("div");
oLi.setAttribute("class","chat_right");
}else{
var ans=`<div class="chat_left_item_1 clearfix">${data.uesr}</div>
<div class="chat_left_item_2">
<div class="chat_left_time clearfix">${data.times}</div>
<div class="chat_left_content clearfix">${data.text}</div>
</div>`;
var oLi=document.createElement("div");
oLi.setAttribute("class","chat_left");
}
oLi.innerHTML=ans;
send_message.append(oLi);
let ele = document.getElementById('chat_middle_item');
ele.scrollTop = ele.scrollHeight;
})
var send_message=document.getElementById("chat_middle_item");
var message=document.getElementById("chat_context_item");
var domBtm=document.getElementById("button");
domBtm.addEventListener("click",function(){
var str=message.value;
var date=new Date();
var hh=date.getHours();
var mm=date.getMinutes();
var ss=date.getSeconds();
var time=hh+':'+mm+':'+ss;
socket.emit('send',{uesr:names,text:str,times:time});
message.value='';
});
</script>
</body>
</html>
sever.js
var express=require('express')
var app=express()
var http=require('http').Server(app)
app.use("/node_modules/",express.static('./node_modules/'))
var io =require('socket.io')(http)
app.get('/chat',function(req,res){
res.sendFile(__dirname+'/chat_message.html')
})
io.on('connection',function(socket){
socket.on('user',function(name){
io.emit('username',name);
})
socket.on('send',function(data){
io.emit('message',data)
})
})
http.listen(9000,function(){
console.log('服务器,9000')
})
在natapp注册免费通道
- 依照window安装教程创建config.int
- 下载客户端
- 将客户端与config.int放在同一个文件夹中
开始运行
- node sever.js
- 打开natapp.exe
- 复制Forwarding http://5qusi2.natappfree.cc
- 访问路由路径 http://5qusi2.natappfree.cc /chat
- /caht为sever.js中自己写的路由