最近刚写了一个在线群聊的功能。
一个是vue页面;
一个是后台服务js;需要先下载ws
<html lang="en">
<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>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style>
.after {
display: flex;
}
.after .left .content {
width: 500px;
height: 300px;
border: solid 1px red;
overflow: scroll;
overflow-x: hidden;
padding: 0 20px;
}
.after .left .content .notice {
text-align: center;
}
.after .left .content .myMessage {
text-align: right;
}
.after .left .content .message {
border: solid 2px black;
display: inline-block;
}
.after .right {
width: 200px;
height: inherit;
border: solid 1px blue;
}
.after .right .isMe {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- 先输入昵称 -->
<div class="before" v-if="!ifEnter">
<input v-model="userName" :value="userName" search enter-button="确定"
placeholder="请输入您的昵称" />
<button @click="settingName">确定</button>
</div>
<!-- 开始群聊 -->
<div class="after" v-else>
<div class="left">
<!-- 聊天内容 -->
<div class="content" id="content">
<div v-for="(item,index) in messageList" :key="index">
<!-- 加入群聊 -->
<div class="notice" v-if="item.type=='addUser'">
<h2>{
{
item.user}}加入群聊</h2>
</div>
<