@@ -63,23 +63,25 @@ export default {
63
63
const wsUrl = process .env .VUE_APP_BASE_API + " /infra/ws" + ' ?token=' + getAccessToken ();
64
64
this .url = wsUrl .replace (" http" , " ws" );
65
65
// 获取用户精简信息列表
66
- const self = this ;
67
66
listSimpleUsers ().then (res => {
68
- self .userList = res .data ;
67
+ this .userList = res .data ;
69
68
});
70
69
},
71
70
methods: {
71
+ /** 发起连接 */
72
72
connect () {
73
73
if (! ' WebSocket' in window ) {
74
74
this .$modal .msgError (" 您的浏览器不支持WebSocket" );
75
75
return ;
76
76
}
77
+ // 建立连接
77
78
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 " ;
81
82
};
82
- this .ws .onmessage = function (event ) {
83
+ // 监听 message 事件
84
+ this .ws .onmessage = (event ) => {
83
85
try {
84
86
const data = event .data
85
87
// 1. 收到心跳
@@ -91,40 +93,44 @@ export default {
91
93
const type = jsonMessage .type
92
94
const content = JSON .parse (jsonMessage .content )
93
95
if (! type) {
94
- self .$modal .msgError (' 未知的消息类型:' + data)
96
+ this .$modal .msgError (' 未知的消息类型:' + data)
95
97
return
96
98
}
97
99
// 2.2 消息类型:demo-message-receive
98
100
if (type === ' demo-message-receive' ) {
99
101
const single = content .single
100
- self .content = self .content + " 接收时间:" + getNowDateTime () + " \n " +
102
+ this .content = this .content + " 接收时间:" + getNowDateTime () + " \n " +
101
103
` 【${ single ? ' 单发' : ' 群发' } 】用户编号(${ content .fromUserId } ):${ content .text } ` + " \n " ;
102
104
return
103
105
}
104
106
// 2.3 消息类型:notice-push
105
107
if (type === ' notice-push' ) {
106
- self .content = self .content + " 接收时间:" + getNowDateTime () + " \n " + ` 【系统通知】:${ content .title } ` + " \n " ;
108
+ this .content = this .content + " 接收时间:" + getNowDateTime () + " \n " + ` 【系统通知】:${ content .title } ` + " \n " ;
107
109
return
108
110
}
109
- self .$modal .msgError (' 未处理消息:' + data)
111
+ this .$modal .msgError (' 未处理消息:' + data)
110
112
} catch (error) {
111
- self .$modal .msgError (' 处理消息发生异常:' + event .data )
113
+ this .$modal .msgError (' 处理消息发生异常:' + event .data )
112
114
console .error (error)
113
115
}
114
116
};
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 " ;
117
120
};
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 " ;
120
124
};
121
125
},
126
+ /** 关闭连接 */
122
127
exit () {
123
128
if (this .ws ) {
124
129
this .ws .close ();
125
130
this .ws = null ;
126
131
}
127
132
},
133
+ /** 发送消息 */
128
134
send () {
129
135
if (! this .ws || this .ws .readyState !== 1 ) {
130
136
this .$modal .msgError (" 未连接到服务器" );
0 commit comments