فهرست منبع

"socket 聊天室"

enenhaha 6 سال پیش
والد
کامیت
e6164d2b63
6فایلهای تغییر یافته به همراه815 افزوده شده و 0 حذف شده
  1. 88 0
      socket_chat/app.js
  2. 5 0
      socket_chat/package-lock.json
  3. 12 0
      socket_chat/package.json
  4. 332 0
      socket_chat/public/css/style.css
  5. 47 0
      socket_chat/public/index.html
  6. 331 0
      socket_chat/public/js/main.js

+ 88 - 0
socket_chat/app.js

@@ -0,0 +1,88 @@
+var express = require('express')                //引入express模块
+var http = require("http")                      //引入http模块
+var app = express()
+app.use('/', express.static('./public'));	//设置静态资源路径
+
+var server = http.createServer(app)
+    server.listen(3000,'0.0.0.0');          //监听端口3000
+
+var io = require('socket.io').listen(server); //引入socket.io模块并绑定到服务器
+
+    
+var AllUser=[];
+
+io.on('connection',function(socket){
+        
+    socket.on("login",function(r){//接收请求 r 为前端发送的参数  r.name  用户名称  r.img 用户头像  
+        let user = {
+            name:r.name,
+            img:r.img,
+            ip:socket.handshake.address,
+            id:r.id,
+            socket:socket
+        }
+        if(r.id=='' || r.id=="undefined"){//如果是新访问的用户是没有id存在cookie的 需要用soucket的id  否则就使用cookie的id
+            user.id = socket.id;
+        }else{
+            socket.id=r.id;
+        }
+        
+        AllUser.push(user);//将定义的变量user添加至全局变量AllUser中  用于显示用户列表的用户信息
+
+        var User = [];//定义一个User变量 将用户列表所需的信息添加进去
+        for (const key in AllUser) {
+            let qq = {
+                name:AllUser[key].name,
+                img:AllUser[key].img,
+                id:AllUser[key].id,
+            };
+            User.push(qq);
+        }
+        socket.emit("login",{ "id": user.id});//发送id给客户端 将id设置至cookie中
+        io.sockets.emit("AllUser",{"AllUser":User});//给所有在线的用户 发送用户列表更新
+
+    });
+
+
+    socket.on("send",function(r){//消息发送 r.accept 接收方id  r.send 发送方id
+        r.date = new Date().toLocaleTimeString();//时间
+
+        if(r.accept=="all"){//如果接收方id为 all  就像所有在线用户发送消息
+            io.sockets.emit("send",r);//向公共聊天室发送消息
+        }else{//否则就是私聊操作
+            socket.emit("send",r);//给当前的用户发送
+
+            for (let i = 0; i < AllUser.length; i++) {
+                if(AllUser[i].id == r.accept){//匹配存储的用户信息 id 与 r.accept 接收者相同 给这个用户发送消息
+                    AllUser[i].socket.emit("send",r);//私聊发送消息
+                    break;//结束循环
+                }
+            }
+
+        }
+        
+    });
+    
+    
+    socket.on('disconnect',function(){//连接断开
+        var index = -1;
+        for (const key in AllUser) {//找到匹配存储的用户信息 将他从存储用户信息变量中删除
+            if(socket.id == AllUser[key].id){
+                AllUser.splice(key,1);// key 为数组的索引值   1为删除的个数
+            }
+        }
+
+        var User = [];
+        for (const key in AllUser) {//定义一个User变量 将用户列表所需的信息添加进去
+            let qq = {
+                name:AllUser[key].name,
+                img:AllUser[key].img,
+                id:AllUser[key].id,
+            };
+            User.push(qq);
+        }
+        io.sockets.emit("AllUser",{"AllUser":User});//更新用户列表信息
+    })
+    
+});
+console.log("Server running: http://127.0.0.1:3000");//打印控制台

+ 5 - 0
socket_chat/package-lock.json

@@ -0,0 +1,5 @@
+{
+  "name": "socket",
+  "version": "1.0.0",
+  "lockfileVersion": 1
+}

+ 12 - 0
socket_chat/package.json

@@ -0,0 +1,12 @@
+{
+  "name": "socket",
+  "version": "1.0.0",
+  "description": "",
+  "main": "app.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {}
+}

+ 332 - 0
socket_chat/public/css/style.css

@@ -0,0 +1,332 @@
+body{
+    margin: 0;
+    padding: 0;
+}
+:focus {
+	outline: 0
+}
+::-webkit-scrollbar-track{background-color:transparent;}
+::-webkit-scrollbar{width:5px;background-color:transparent;}
+::-webkit-scrollbar-thumb{background-color:#b8b8b8;border-radius:15px;}
+
+
+.chat_room {
+	width: 700px;
+	height: 595px;
+	border: 1px solid #cacaca;
+	/* border-radius: 4px; */
+	margin: auto;
+	margin-top: 50px;
+	position: relative;
+}
+
+.chat_room>.list {
+	width: 160px;
+	height: 100%;
+	float: left;
+	border-right: 1px solid #e7e7e7;
+}
+.chat_room .list .list_1{
+    box-sizing: border-box;
+    padding: 8px;
+    display: inline-block;
+    width: 100%;
+    position: relative;
+}
+.chat_room .list .on{
+    background: #e8e8e8;
+}
+.chat_room .list .list_1:hover{
+    background: #e8e8e8;
+}
+.chat_room .list .img {width: 38px;background: #ddd;float: left;height: 38px;border-radius: 55px;overflow: hidden;cursor: pointer;}
+.chat_room .list .img img{
+    width: 100%;
+    height: 100%;
+}
+.chat_room .list .title {font-size: 14px;text-indent: 9px;color: #353535;}
+
+.chat_room .list .info {font-size: 12px;color: #8f8f8f;text-indent: 13px;padding-top: 2px;}
+
+.chat_room>.content {
+	float: right;
+	width: calc(100% - 161px);
+	height: 75%;
+	border-bottom: 1px solid #e6e6e6;
+	overflow: auto;
+	position: absolute;
+	right: 0;
+	background: #fff;
+}
+
+.chat_room .input {
+	float: left;
+	width: calc(100% - 161px);
+	height: 25%;
+	position: absolute;
+	bottom: 0;
+	right: 0;
+}
+.chat_room .input .input_content{
+    width:100%;
+    height: 66%;
+    padding: 8px;
+    box-sizing: border-box;
+    overflow: overlay;
+    margin-top: 4px;
+    border: 0;
+    display: block;
+    resize: none;
+}
+.chat_room .input input {
+    position: absolute;
+    bottom: 15px;
+    right: 15px;
+    border: 0;
+    border-radius: 2px;
+    padding: 4px 11px;
+    color: #fff;
+    cursor: pointer;
+    background: rgb(38,131,245);
+}
+
+
+
+
+
+
+.chat_room .content .me{
+    display: inline-block;
+    padding: 13px 12px;
+    box-sizing: border-box;
+    position: relative;
+    width: 100%;
+}
+.chat_room .content .me .img{
+    width: 39px;
+    height: 39px;
+    background: #ddd;
+    border-radius: 55px;
+    float: right;
+    cursor: pointer;
+    overflow: hidden;
+}
+.chat_room .content .me .img img{
+
+width: 100%;
+
+height: 100%;
+}
+.chat_room .content .me p.name{
+    float: right;
+    margin: -2px 9px 0 0;
+    color: #2d2d2d;
+    font-size: 15px;
+    max-width: 150px;
+    overflow: hidden;
+    word-break: keep-all;
+}
+.chat_room .content .me p.content{
+    float: right;
+    border: 0;
+    margin: -18px 49px 0 0;
+    width: auto;
+    max-width: 61%;
+    height: auto;
+    text-align: left;
+    word-wrap: break-word;
+    background: rgb(38,131,245);
+    border-radius: 9px;
+    box-sizing: border-box;
+    padding: 9px 17px;
+    color: #f4f4f4;
+}
+
+.chat_room .content .me p.content span{
+    display: block;
+    clear: both;
+    width: 100%;
+    text-align: right;
+    color: #f4f4f4;
+    margin-top: 8px;
+    font-size: 13px;
+}
+
+
+
+
+.chat_room .content .other{
+    display: inline-block;
+    padding: 13px 12px;
+    box-sizing: border-box;
+    position: relative;
+    width: 100%;
+}
+.chat_room .content .other .img{
+    width: 39px;
+    height: 39px;
+    background: #ddd;
+    border-radius: 55px;
+    float: left;
+    cursor: pointer;
+    overflow: hidden;
+}
+.chat_room .content .other .img img{
+
+width: 100%;
+
+height: 100%;
+}
+.chat_room .content .other p.name{
+    float: left;
+    margin: -2px 0px 0 10px;
+    color: #2d2d2d;
+    font-size: 15px;
+    max-width: 150px;
+    overflow: hidden;
+    word-break: keep-all;
+}
+.chat_room .content .other p.content{
+    float: left;
+    border: 0;
+    margin: -18px 9px 0 49px;
+    width: auto;
+    max-width: 61%;
+    height: auto;
+    text-align: left;
+    word-wrap: break-word;
+    background: rgb(38,131,245);
+    border-radius: 9px;
+    box-sizing: border-box;
+    padding: 9px 17px;
+    color: #f4f4f4;
+}
+
+.chat_room .content .other p.content span{
+    display: block;
+    clear: both;
+    width: 100%;
+    text-align: left;
+    color: #f4f4f4;
+    margin-top: 8px;
+    font-size: 13px;
+}
+
+
+
+
+
+.setting{
+    position: fixed;
+    top: 0;
+    left: 0;
+    width: 100%;
+    height: 100%;
+    background: rgba(0, 0, 0, 0.67);
+}
+.setting .setting_main{
+    width: 471px;
+    height: 290px;
+    background: #fff;
+    margin: auto;
+    position: relative;
+    top: 20%;
+    border-radius: 7px;
+    box-sizing: border-box;
+    padding: 16px;
+}
+.setting .setting_main .img{
+    width: 90px;
+    height: 90px;
+    background: #ddd;
+    border-radius: 50%;
+    margin: auto;
+    margin-top: 10px;
+    overflow: hidden;
+    cursor: pointer;
+    position: relative;
+}
+.setting .setting_main .img:hover span{
+    opacity: 1;
+}
+.setting .setting_main .img img{
+    width: 100%;
+    height: 100%;
+}
+.setting .setting_main .img span{
+    font-size: 13px;
+    display: block;
+    color: #2b2929;
+    transition: 0.3s;
+    position: absolute;
+    top: 32px;
+    width: 100%;
+    opacity: 1;
+    text-align: center;
+}
+.setting .setting_main .info{
+    margin-top: 28px;
+    text-align: center;
+}
+.setting .setting_main .info span{
+    margin-right: 10px;
+}
+.setting .setting_main .info input{
+    width: 150px;
+    height: 25px;
+    border: 1px solid #000;
+    text-indent: 7px;
+}
+
+.setting .setting_main .ok{
+    width: 248px;
+    height: 39px;
+    display: block;
+    border: 1px solid #333333;
+    color: #616266;
+    margin: auto;
+    cursor: pointer;
+    background: #fff;
+    margin-top: 20px;
+}
+
+.prompt{
+    font-size: 55px;
+    color: #f00;
+    position: absolute;
+    top: -45px;
+    right: 8px;
+}
+
+
+
+.chat_room .user_list{
+
+position: absolute;
+
+top: -1px;
+
+right: -154px;
+
+width: 152px;
+
+height: 65%;
+
+box-sizing: border-box;
+
+/* padding: 5px 10px; */
+
+background: #eee;
+}
+.chat_room .user_list .title{
+
+padding: 10px 10px;
+}
+.chat_room .user_list .list{
+    
+width: 100%;
+    
+height: calc(100% - 41px);
+    
+overflow: auto;
+}

+ 47 - 0
socket_chat/public/index.html

@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="utf-8" />
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <title>聊天室</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />
+    <script src="socket.io/socket.io.js"></script>
+</head>
+<body>
+    <div class="chat_room">
+        <div class="list">
+            <div class="list_1 on" id="all" onclick="char_list(this)">
+                <div class="img"><img src="" alt=""></div>
+                <div class="title">公共聊天室</div>
+                <div class="info"></div>
+            </div>
+        </div>
+        <div class="content" id="all"></div>
+        <div class="input">
+            <textarea class="input_content"></textarea>
+            <input type="button" value="发送" onclick="send(this)">
+        </div>
+        <div class="user_list">
+            <div class="title">用户列表</div>
+            <div class="list">
+            </div>
+        </div>
+    </div>
+    <div class="setting" style="display: none;">
+        <div class="setting_main">
+            <div class="title">设置</div>
+            <div class="img"onclick="this.querySelector('input').click()">
+                <img src="" alt="">
+                <input type="file" hidden accept="image/gif, image/jpeg,image/png" onchange="changepic(this,document.querySelector('.setting .img img'))">
+                <span>点击更改头像</span>
+            </div>
+            <div class="info">
+                <span>名称:</span><input type="text" class="name">
+            </div>
+            <input type="button" value="确定" class="ok">
+        </div>
+    </div>
+    <script src="js/main.js"></script>
+</body>
+</html>

+ 331 - 0
socket_chat/public/js/main.js

@@ -0,0 +1,331 @@
+//设置 cookie
+function setCookie(c_name,value,expiredays){  // 名称 值 时间
+    var exdate=new Date()
+    exdate.setDate(exdate.getDate()+expiredays)
+    document.cookie=c_name+ "=" +escape(value)+
+    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
+}
+//获取 cookie
+function getCookie(c_name){ // 名称
+    if (document.cookie.length>0)
+    {
+    c_start=document.cookie.indexOf(c_name + "=")
+    if (c_start!=-1)
+        { 
+        c_start=c_start + c_name.length+1 
+        c_end=document.cookie.indexOf(";",c_start)
+        if (c_end==-1) c_end=document.cookie.length
+        return unescape(document.cookie.substring(c_start,c_end))
+        } 
+    }
+    return ""
+}
+
+var socket='';
+Socket = function(){
+    this.socket='';
+    this.init();
+    this.content();
+}
+Socket.prototype={
+    init:function(){
+        this.socket = io.connect();
+    },
+    content:function(){
+        if(getCookie("name")!=''){
+            this.socket.emit('login',{"name":getCookie("name"),"img":localStorage.getItem('img'),"id":getCookie("id")});
+        }
+
+        this.socket.on('login',function(r){
+            setCookie('id',r.id,10);
+        });
+
+        this.socket.on('AllUser',function(r){
+            document.querySelector(".chat_room .user_list .list").innerHTML='';
+            var html='';
+    
+            for (let i = 0; i < r.AllUser.length; i++) {
+                if(getCookie("id") == r.AllUser[i].id){
+                    var dd = "color:red;";
+                }else{
+                    var dd = "";
+                }
+                html += `
+                <div class="list_1" id="`+r.AllUser[i].id+`" title="`+r.AllUser[i].name+`">
+                    <div class="img" onclick="user_info(this,'`+r.AllUser[i].id+`')"><img src="`+r.AllUser[i].img+`" alt=""></div>
+                    <div class="name" style="`+dd+`padding-bottom: 9px; height: 21px;overflow: hidden;box-sizing: border-box;margin-top: 6px;margin-left: 49px;">`+r.AllUser[i].name+`</div>
+                </div>`;
+            }
+            document.querySelector(".chat_room .user_list .list").innerHTML=html;
+
+        });
+        
+        
+        this.socket.on('send',function(r){//接受信息
+            
+            if(r.id == getCookie("id")){//判断发送消息的对象是谁
+                var type = "me";
+            }else{
+                var type = "other";
+            }
+
+            if(r.accept=="all"){//判断是否为公共聊天室
+
+                let content = document.querySelectorAll(".chat_room>.content");
+                for (let i = 0; i < content.length; i++) {
+                    if(content[i].getAttribute("id") == r.accept){
+                        chat_information(type,r.id,r.img,r.name,r.content,r.date,content[i]);
+                        break;
+                    }
+                }
+
+            }else{//私聊
+
+                var bool = true;
+                let length = document.querySelectorAll(".chat_room>.list .list_1")
+                for (let i = 0; i < length.length; i++) {
+                    if(length[i].getAttribute("id") == r.accept||length[i].getAttribute("id") == r.send){//判断是否有聊天用户列表
+                        bool=false;
+                    }
+                }
+
+                if(bool){
+                    private_chat('',0,r.name,r.img,r.id);//私聊操作 显示用户列表 聊天框
+                }
+
+
+                let content = document.querySelectorAll(".chat_room>.content");
+                for (let i = 0; i < content.length; i++) {
+                    if(content[i].getAttribute("id") == r.accept||content[i].getAttribute("id") == r.send){//找到聊天框
+                        chat_information(type,r.id,r.img,r.name,r.content,r.date,content[i]);//显示消息内容
+                        break;
+                    }
+                }
+
+
+            }
+
+            var list = document.querySelectorAll(".chat_room>.list>.list_1");
+            for (let i = 0; i < list.length; i++) {
+                if(list[i].getAttribute("id") == r.send||list[i].getAttribute("id") == r.accept){
+                    if(list[i].className!="list_1 on"){//接受信息的框如果不是在现在显示的  就加个红点提示有消息
+                        if(!list[i].querySelector(".prompt")){
+                            var prompt = document.createElement("div");
+                            prompt.className = "prompt";
+                            prompt.innerHTML = '.';
+                            list[i].appendChild(prompt);
+                        }
+                    }
+                    break;
+                }
+            }
+
+
+        });
+    }
+}
+
+
+
+if(getCookie("name")==''||localStorage.getItem('img')==''){//判断是否有设置名称和头像
+    document.querySelector(".setting .img img").src=localStorage.getItem('img');
+    document.querySelector(".setting .info .name").value=getCookie("name");
+    document.querySelector(".setting").style.display='block';
+}else{
+    document.querySelector("body").removeChild(document.querySelector(".setting"));
+    socket = new Socket();//new 一个socket对象
+}
+
+
+
+
+var chant_id="all";//当前聊天室id
+
+
+changepic=function(This,img) {//头像上传后执行的函数  
+    var reads= new FileReader();
+    f=This.files[0];
+    reads.readAsDataURL(f);
+
+    fileSize =This.files[0].size;
+    var size = fileSize / 1024;
+    var fileMaxSize = 1024;//设置头像大小不能超过300K
+    if (size > fileMaxSize) {
+        alert("文件大小不能大于1M!");
+        This.value = "";
+        return false;
+    }else if (size <= 0) {
+        alert("文件大小不能为0M!");
+    }
+    reads.onload=function (e) {
+        img.src=this.result;
+    }
+}
+
+//  type 聊天类型id   id 聊天id   img 头像   name 名称  content 内容  date 时间 create_obj 创建在哪个div聊天框中
+chat_information=function(type,id,img,name,content,date,create_obj){//添加消息内容
+
+    var div = document.createElement("div");
+    div.className=type;
+    div.innerHTML=`
+        <div class="img" id="`+id+`" onclick="user_info(this,'`+id+`')">
+            <img src="`+img+`" alt="">
+        </div>
+        <p class="name">`+name+`</p><br/><br/>
+        <p class="content">
+            `+decodeURI(content)+`
+            <span class="date">`+date+`</span>
+        </p>`;
+    create_obj.appendChild(div);
+    create_obj.scrollTop = create_obj.scrollHeight;//将聊天框滚动到最底部
+}
+
+issetting=function(){//判断是否有设置头像 名称
+    if(document.querySelector(".ok")){
+        document.querySelector(".ok").onclick=function(){
+            var img  = document.querySelector(".setting .img img").src;
+            var name = document.querySelector(".setting_main .info .name").value;
+            if(img==''||name==''){
+                alert("不能为空,请重新输入!");
+                return;
+            }
+            setCookie('name',name,10);      //设置cookie
+            localStorage.setItem('img',img);//将头像放置到 localStorage中
+            alert("设置成功!");
+            document.querySelector("body").removeChild(document.querySelector(".setting"));
+            socket = new Socket();//new 一个socket对象
+        }
+    }
+}
+issetting();
+
+send=function(This){//发送消息
+    if(This.parentNode.querySelector(".input_content").value.trim()==''){
+        This.parentNode.querySelector(".input_content").value='';
+        return false;
+    }
+    var type = chant_id;//获取当前聊天的id
+    var img  = localStorage.getItem('img');//获取头像
+    var name = getCookie("name");//获取用户名
+    var content = This.parentNode.querySelector(".input_content").value;//获取聊天的内容
+    content = encodeURI(content);//将聊天的内容url编码
+    var DATE =new Date();
+    var date = DATE.getHours()+":"+DATE.getMinutes()+":"+DATE.getSeconds();//时间
+
+    This.parentNode.querySelector(".input_content").value='';//清空聊天框内容
+
+    socket.socket.emit("send",{"accept":type,"send":getCookie("id"),'id':getCookie("id"),"img":img,"name":name,"content":content})//发送给请求给服务器
+}
+
+user_info=function(This,id){//点击头像 会显示该用户的信息 可进行私聊
+    if(getCookie("id")==id){//如果点击的是自己的头像 就不显示
+        alert("自己");
+        return;
+    }
+    var div = document.createElement("div");
+    div.className="setting";
+    div.style.display='block';
+    var html=`
+        <div class="setting_main" style="height: 250px;">
+            <div class="img">
+                <img src="`+This.querySelector("img").src+`"/>
+            </div>
+            <div onclick="document.querySelector('body').removeChild(document.querySelector('.setting'))" style="position: absolute;top: 9px;right: 11px;color: #757575; cursor: pointer;">X</div>
+            <div style="text-align: center;margin-top:10px;">`+This.parentNode.querySelector(".name").innerHTML+`</div>
+            <input type="button" value="私聊" class="ok" onclick="private_chat(this,1,'`+This.parentNode.querySelector(".name").innerHTML+`','`+This.querySelector("img").src+`','`+id+`')">
+        </div>`;
+    div.innerHTML=html;
+    document.querySelector("body").appendChild(div);
+
+}
+
+private_chat=function(This,bool,name,img,id){
+    //私聊操作  This 触发对象  bool (判断是私聊的  还是被私聊的 1是私聊的 0被私聊)  name 名称  img 头像  id
+
+    var qq = true;
+    let list_1 = document.querySelectorAll(".chat_room>.list .list_1");
+    for (let i = 0; i < list_1.length; i++) {
+        if(list_1[i].getAttribute("id") == id){//判断聊天列表中 是否有该用户
+            qq=false;
+        }
+    }
+
+    if(qq){//聊天列表中没有改用户 创建聊天列表 创建聊天框
+        var div = document.createElement("div");
+        div.className = 'list_1';
+        div.id = id;
+        div.onclick=function(){char_list(this);};
+        div.innerHTML=`
+        <div class="img"><img src="`+img+`" alt=""></div>
+        <div class="title">`+name+`</div>
+        <div class="info"></div>`;
+
+        var content_div = document.createElement("div");
+        content_div.className = "content";
+        content_div.id = id;
+        content_div.style.display = "none";
+
+        document.querySelector(".chat_room .list").appendChild(div);
+        document.querySelector(".chat_room").appendChild(content_div);
+    }
+
+    if(bool==1){//如果是主动私聊的 就将跟该用户的聊天列表和聊天框显示出来
+        chant_id = id;
+        char_list_on(id)
+        chat_room_content_on(id)
+    }
+
+    if(document.querySelector(".setting")){//删除查看用户信息的弹窗
+        document.body.removeChild(document.querySelector(".setting"));
+    }
+
+}
+
+char_list_on=function(id){//切换至某个用户列表显示
+    let list_1 = document.querySelectorAll(".chat_room .list .list_1");
+    for (let i = 0; i < list_1.length; i++) {
+        list_1[i].className = "list_1";
+        if(list_1[i].getAttribute("id") == id){
+            list_1[i].className = "list_1 on";
+            if(list_1[i].querySelector(".prompt")){
+                list_1[i].removeChild(list_1[i].querySelector(".prompt"));
+            }
+        }
+    }
+}
+
+chat_room_content_on=function(id){//切换至某个用户聊天框显示
+    let content = document.querySelectorAll(".chat_room>.content");
+    for (let i = 0; i < content.length; i++) {
+        content[i].style.display = "none";
+        if(content[i].getAttribute("id") == id){
+            content[i].style.display = "block";
+        }
+    }
+}
+
+char_list=function(This){//列表点击切换 并切换聊天框
+    chant_id=This.getAttribute("id");
+    var length = document.querySelectorAll(".chat_room .list .list_1");
+    for (let i = 0; i < length.length; i++) {
+        length[i].className = "list_1";
+    }
+    This.className= "list_1 on";
+
+    if(This.querySelector(".prompt")){
+        This.removeChild(This.querySelector(".prompt"));
+    }
+
+    chat_room_content_on(This.getAttribute("id"));//切换至某个用户聊天框显示
+
+}
+
+
+document.querySelector(".input_content").onfocus=function(){//聊天输入框的回车事件
+    document.onkeyup = function (e) {//按键信息对象以函数参数的形式传递进来了,就是那个e  
+        var code = e.charCode || e.keyCode;  //取出按键信息中的按键代码(大部分浏览器通过keyCode属性获取按键代码,但少部分浏览器使用的却是charCode)  
+        if (code == 13) {//code 为 13 是回车
+            document.querySelector(".chat_room .input input").click();
+        }
+    }  
+}