修改己方消息样式,以及对后端json字符串解析

This commit is contained in:
Cool 2023-11-26 14:49:01 +08:00
parent b06fc66df7
commit cd0091292e
1 changed files with 263 additions and 311 deletions

View File

@ -1,191 +1,137 @@
<template> <template>
<div class="home"> <div class="home">
<div class="header"> <div class="header">
<h1 class="title">聊天室</h1> <h1 class="title">聊天室</h1>
<hr class="divide"> <hr class="divide">
</div>
<div class="body" id="chat">
<div v-for="(message, index) in messageList" :key="index">
<div class="word" v-if="!(message.nickname == 'COOL')">
<div class="information">
<p>{{ message.nickname }} {{ message.sendTime }}</p>
</div>
<div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
</div>
</div> </div>
<div class="body" id="chat">
<div v-for="(message, index) in messageList" :key="index">
<div class="word" v-if="!message.isMine"> <div class="word-my" v-else>
<div class="information"> <div class="outer">
<p>{{ message.nickname }} {{ message.sendTime }}</p> <div class="information">
</div> <p> {{ message.sendTime }} {{ message.nickname }} </p>
<div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
</div>
</div>
<div class="word-my" v-else>
<div class="outer">
<div class="information">
<p>{{ message.sendTime }} {{ message.nickname }} </p>
</div>
<div class="info">
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
<el-avatar :src="message.pic" class="head"></el-avatar>
</div>
</div>
</div>
</div> </div>
<div class="info">
<el-avatar :src="message.pic" class="head"></el-avatar>
<div class="message">
<div class="detail">
{{ message.message }}
</div>
</div>
</div>
</div>
</div> </div>
<div class="footer"> </div>
<div class="footer1">
<el-input type="textarea" :rows="8" resize="none" placeholder="" v-model="textarea" class="textarea">
</el-input>
<el-button class="button" @click="send(textarea)">发送</el-button>
</div>
</div>
</div> </div>
<div class="footer">
<div class="footer1">
<el-input type="textarea" :rows="8" resize="none" placeholder="" v-model="textarea" class="textarea">
</el-input>
<el-button class="button" @click="send(textarea)">发送</el-button>
</div>
</div>
</div>
</template> </template>
<script> <script>
let socket; let socket;
export default { export default {
data() { data() {
return { return {
messageList: [ messageList: [],
textarea: "",
myName: '',
myPic: '',
{ };
nickname: 'Cool', },
pic: require("@/assets/img/cool.jpg"), mounted() {
sendTime: '2023/11/20 15:23:18', var container = document.getElementById('chat');
message: '我怎么把文件放在服务器上,然后在公网对他进行访问?', container.scrollTop = container.scrollHeight;
isMine: false, this.init()
}, },
methods: {
send() {
if (!this.textarea) {
this.$message({ type: 'warning', message: "请输入内容" })
} else {
let SendMessage = {
nickname: 'COOL',
pic: "1",
sendTime: '2023/11/20 18:23:18',
message: this.textarea,
}
socket.send(JSON.stringify(SendMessage));
this.textarea = "";
console.log(this.messageList, "这是send")
}
},
{ init() {
nickname: 'COOL', const that = this
pic: require("@/assets/img/cool.jpg"), if (typeof (WebSocket) === undefined) {
sendTime: '2023/11/20 18:23:18', console.log("您的浏览器不支持WebSocket");
message: '先xx后xx再xx', } else {
isMine: true, console.log("您的浏览器支持WebSocket");
}, let socketUrl = "ws://localhost:8080/chat/get";
{ if (socket != null) {
nickname: 'COOL', socket.close();
pic: require("@/assets/img/cool.jpg"), socket = null;
sendTime: '2023/11/20 18:23:18', }
message: '先xx后xx再xx',
isMine: true,
},
{
nickname: 'COOL',
pic: require("@/assets/img/cool.jpg"),
sendTime: '2023/11/20 18:23:18',
message: '先xx后xx再xx',
isMine: true,
}, {
nickname: 'COOL',
pic: require("@/assets/img/cool.jpg"),
sendTime: '2023/11/20 18:23:18',
message: '先xx后xx再xx',
isMine: true,
}, {
nickname: 'COOL',
pic: require("@/assets/img/cool.jpg"),
sendTime: '2023/11/20 18:23:18',
message: '先xx后xx再xx',
isMine: true,
},
{
nickname: 'COOL',
pic: require("@/assets/img/cool.jpg"),
sendTime: '2023/11/20 18:23:18',
message: '先xx后xx再xx',
isMine: true,
}, {
nickname: 'COOL',
pic: require("@/assets/img/cool.jpg"),
sendTime: '2023/11/20 18:23:18',
message: '先xx后xx再xx',
isMine: false,
},
],
textarea: "",
myName:'',
myPic:'',
socket = new WebSocket(socketUrl);
socket.onopen = function () {
console.log("websocket已打开");
}; };
socket.onmessage = function (msg) {
var data = JSON.parse(msg.data);
that.messageList = data
console.log(that.messageList[1].message, "nickname")
console.log(that.messageList, 2)
}
socket.onerror = function () {
console.log("websocket发生了错误");
}
socket.onclose = function () {
console.log("websocket已关闭");
};
}
}, },
mounted() {
var container = document.getElementById('chat'); SendMessage(textarea) {
container.scrollTop = container.scrollHeight; //
this.init() console.log(textarea);
}, },
methods: {
send(){
if (!this.textarea) {
this.$message({type: 'warning', message: "请输入内容"})
} else {
let SendMessage={
nickname: 'COOL',
pic: "1",
sendTime: '2023/11/20 18:23:18',
message: this.textarea,
}
socket.send(SendMessage);
this.textarea = "";
console.log(this.messageList,"这是send")
}
},
init() { },
const that=this
if (typeof (WebSocket) === undefined) {
console.log("您的浏览器不支持WebSocket");
} else {
console.log("您的浏览器支持WebSocket");
let socketUrl = "ws://localhost:8080/chat/get";
if (socket != null) {
socket.close();
socket = null;
}
socket = new WebSocket(socketUrl);
socket.onopen = function () {
console.log("websocket已打开");
};
socket.onmessage = function (msg) {
console.log(msg.data,1)
console.log(that.messageList,3)
that.messageList = msg.data
console.log(that.messageList,2)
}
socket.onerror = function () {
console.log("websocket发生了错误");
}
socket.onclose = function () {
console.log("websocket已关闭");
};
}
},
SendMessage(textarea) {
//
console.log(textarea);
},
},
}; };
@ -195,172 +141,178 @@ export default {
.home { .home {
.header { .header {
.title { .title {
margin-left: 20px; margin-left: 20px;
font-size: 20px; font-size: 20px;
color: #000000; color: #000000;
}
.divide {
width: 100%;
margin-top: 10px;
border: 1;
height: 2px;
background-color: rgb(231, 231, 231);
border: none;
}
}
.body {
// overflow: hidden;
overflow: auto;
/* 可选:设置最大高度以限制滚动区域 */
max-height: 350px;
.word {
.information {
margin-left: 20px;
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
.head {
margin-top: 10px;
margin-right: 10px;
} }
.divide { .message {
width: 100%; padding-left: 7px;
padding-right: 7px;
margin-left: 10px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-right-color: #ffffff;
left: -16px;
}
}
}
}
.word-my {
//
display: flex;
// justify-content: flex-end;
direction: rtl;
.outer {
.information {
margin-left: 20px;
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
position: relative;
right: 10px;
.head {
margin-top: 10px; margin-top: 10px;
border: 1; position: relative;
height: 2px; right: 10px;
background-color: rgb(231, 231, 231); }
border: none;
} .message {
}
.body { padding-right: 7px;
// overflow: hidden; padding-left: 7px;
overflow: auto; margin-right: 28px;
/* 可选:设置最大高度以限制滚动区域 */ margin-top: 10px;
max-height: 350px; position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: #95ec69;
.word { max-width: 70%;
.information { min-height: 40px;
margin-left: 20px; //
margin-top: 20px;
font-size: 12px;
color: rgb(32, 179, 86);
}
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
.head {
margin-top: 10px;
margin-right: 10px;
}
.message {
padding-left: 7px;
padding-right: 7px;
margin-left: 10px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-right-color: #ffffff;
left: -16px;
}
}
}
}
.word-my {
//
display: flex; display: flex;
justify-content: flex-end; align-items: center;
justify-content: center;
.detail::before {
position: absolute;
.outer { display: inline-block;
content: "";
.information { width: 0px;
margin-left: 20px; height: 0px;
margin-top: 20px; border: 8px solid transparent;
font-size: 12px; top: 10px;
color: rgb(32, 179, 86); /*移到中间*/
border-left-color: #95ec69;
} right: -16px;
.info {
margin-left: 20px;
margin-top: 10px;
font-size: 16px;
display: flex;
.head {
margin-top: 10px;
margin-right: 10px;
}
.message {
padding-right: 7px;
padding-left: 7px;
margin-right: 18px;
margin-top: 10px;
position: relative;
font-size: 16px;
color: rgb(0, 0, 0);
background-color: #95ec69;
max-width: 70%;
min-height: 40px;
//
display: flex;
align-items: center;
justify-content: center;
.detail::before {
position: absolute;
display: inline-block;
content: "";
width: 0px;
height: 0px;
border: 8px solid transparent;
top: 10px;
/*移到中间*/
border-left-color: #95ec69;
right: -16px;
}
}
}
} }
}
} }
}
} }
.footer { }
.footer1 { .footer {
position: fixed;
bottom: 60px;
width: 100%;
padding: 10px; .footer1 {
position: fixed;
bottom: 60px;
width: 100%;
padding: 10px;
.textarea { .textarea {
color: rgb(32, 179, 86); color: rgb(32, 179, 86);
width: 80%; width: 80%;
} }
.button {
position: fixed;
right: 40px;
bottom: 20px;
background-color: rgb(233, 233, 233);
}
}
.button {
position: fixed;
right: 40px;
bottom: 20px;
background-color: rgb(233, 233, 233);
}
} }
}
} }
</style> </style>