- Web聊天室({{ chatUser }})
+ ({{ chatUser }})
30) {
//暂停定时器
this.stopInterval();
- alert("连接失败,请重试!");
+ confirm("连接失败,请重试!");
router.push("/user");
}
}
@@ -285,7 +285,6 @@ export default {
// 将解密后的 WordArray 转换为 UTF-8 字符串
const decryptedText = decrypted.toString(CryptoJS.enc.Utf8);
- console.log("decryptedText: " + decryptedText,secretKey, iv,encryptedData);
return decryptedText;
} catch (error) {
// 捕获并处理异常
@@ -306,7 +305,7 @@ export default {
"&token=" +
this.tokenData.token;
- console.log("socketUrl:", socketUrl);
+ // console.log("socketUrl:", socketUrl);
if (this.socket != null) {
this.socket.close();
this.socket = null;
@@ -315,19 +314,29 @@ export default {
this.socket = new WebSocket(socketUrl);
//打开事件
this.socket.onopen = function () {
- alert("websocket已打开");
+ confirm("连接成功");
+ this.loading = false;
};
this.socket.onerror = (error) => {
console.error("WebSocket Error:", error);
};
+ //关闭事件
+ this.socket.onclose = function () {
+ alert("连接已关闭!");
+ router.push("/user");
+ };
// 浏览器端收消息,获得从服务端发送过来的文本消息
this.socket.onmessage = async function (msg) {
- console.log("收到数据====" + msg.data);
+ //console.log("收到数据====" + msg.data);
let data = JSON.parse(msg.data); // 对收到的json数据进行解析, 类似这样的:
// 如果服务器端发送过来的json数据 不包含 users 这个key,那么发送过来的就是聊天文本json数据
- data.data=await _this.decryptAES(data.data, _this.imKey, _this.imKey)
+ data.data = await _this.decryptAES(
+ data.data,
+ _this.imKey,
+ _this.imKey
+ );
_this.messages.push(data);
- console.log("收到数据====" + msg.data);
+ //console.log("收到数据====" + msg.data);
// 构建消息内容
_this.createContent(_this.to_user_name, null, data.data);
};
@@ -336,7 +345,6 @@ export default {
},
// 生命周期钩子,在组件挂载完成后被调用
mounted() {
- console.log("chatUser:", this.chatUser);
this.to_user_id = localStorage.getItem("to_user_id");
this.to_user_name = localStorage.getItem("to_user_name");
this.chatUser = this.to_user_name;
@@ -345,9 +353,10 @@ export default {
//this.connectWebSocket();
},
// 生命周期钩子,在组件卸载之前被调用
- beforeDestroy() {
+ onUnmounted() {
this.stopInterval();
this.socket.close();
+ this.loading = false;
},
};