Skip to content

Commit 97cf3aa

Browse files
committed
websocket:界面 self 改 this
1 parent 9d0c9f4 commit 97cf3aa

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/views/infra/webSocket/index.vue

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,25 @@ export default {
6363
const wsUrl = process.env.VUE_APP_BASE_API + "/infra/ws" + '?token=' + getAccessToken();
6464
this.url = wsUrl.replace("http", "ws");
6565
// 获取用户精简信息列表
66-
const self = this;
6766
listSimpleUsers().then(res => {
68-
self.userList = res.data;
67+
this.userList = res.data;
6968
});
7069
},
7170
methods: {
71+
/** 发起连接 */
7272
connect() {
7373
if (!'WebSocket' in window) {
7474
this.$modal.msgError("您的浏览器不支持WebSocket");
7575
return;
7676
}
77+
// 建立连接
7778
this.ws = new WebSocket(this.url);
78-
const self = this;
79-
this.ws.onopen = function (event) {
80-
self.content = self.content + "\n**********************连接开始**********************\n";
79+
// 监听 open 事件
80+
this.ws.onopen = (event) => {
81+
this.content = this.content + "\n**********************连接开始**********************\n";
8182
};
82-
this.ws.onmessage = function (event) {
83+
// 监听 message 事件
84+
this.ws.onmessage = (event) => {
8385
try {
8486
const data = event.data
8587
// 1. 收到心跳
@@ -91,40 +93,44 @@ export default {
9193
const type = jsonMessage.type
9294
const content = JSON.parse(jsonMessage.content)
9395
if (!type) {
94-
self.$modal.msgError('未知的消息类型:' + data)
96+
this.$modal.msgError('未知的消息类型:' + data)
9597
return
9698
}
9799
// 2.2 消息类型:demo-message-receive
98100
if (type === 'demo-message-receive') {
99101
const single = content.single
100-
self.content = self.content + "接收时间:" + getNowDateTime() + "\n" +
102+
this.content = this.content + "接收时间:" + getNowDateTime() + "\n" +
101103
`${single ? '单发' : '群发'}】用户编号(${content.fromUserId}):${content.text}` + "\n";
102104
return
103105
}
104106
// 2.3 消息类型:notice-push
105107
if (type === 'notice-push') {
106-
self.content = self.content + "接收时间:" + getNowDateTime() + "\n" + `【系统通知】:${content.title}` + "\n";
108+
this.content = this.content + "接收时间:" + getNowDateTime() + "\n" + `【系统通知】:${content.title}` + "\n";
107109
return
108110
}
109-
self.$modal.msgError('未处理消息:' + data)
111+
this.$modal.msgError('未处理消息:' + data)
110112
} catch (error) {
111-
self.$modal.msgError('处理消息发生异常:' + event.data)
113+
this.$modal.msgError('处理消息发生异常:' + event.data)
112114
console.error(error)
113115
}
114116
};
115-
this.ws.onclose = function (event) {
116-
self.content = self.content + "**********************连接关闭**********************\n";
117+
// 监听 close 事件
118+
this.ws.onclose = (event) => {
119+
this.content = this.content + "**********************连接关闭**********************\n";
117120
};
118-
this.ws.error = function (event) {
119-
self.content = self.content + "**********************连接异常**********************\n";
121+
// 监听 error 事件
122+
this.ws.error = (event) => {
123+
this.content = this.content + "**********************连接异常**********************\n";
120124
};
121125
},
126+
/** 关闭连接 */
122127
exit() {
123128
if (this.ws) {
124129
this.ws.close();
125130
this.ws = null;
126131
}
127132
},
133+
/** 发送消息 */
128134
send() {
129135
if (!this.ws || this.ws.readyState !== 1) {
130136
this.$modal.msgError("未连接到服务器");

0 commit comments

Comments
 (0)