提交 | 用户 | 时间
|
759b1c
|
1 |
<template> |
H |
2 |
<div class="app-container"> |
|
3 |
<el-form label-width="120px"> |
|
4 |
<el-row type="flex" :gutter="0"> |
|
5 |
<el-col :sm="12"> |
|
6 |
<el-form-item label="WebSocket地址" size="small"> |
|
7 |
<el-input v-model="url" type="text"/> |
|
8 |
</el-form-item> |
|
9 |
</el-col> |
|
10 |
<el-col :offset="1"> |
|
11 |
<el-form-item label="" label-width="0px" size="small"> |
|
12 |
<el-button @click="connect" type="primary" :disabled="ws&&ws.readyState===1"> |
|
13 |
{{ ws && ws.readyState === 1 ? "已连接" : "连接" }} |
|
14 |
</el-button> |
|
15 |
<el-button @click="exit" type="danger">断开</el-button> |
|
16 |
</el-form-item> |
|
17 |
</el-col> |
|
18 |
</el-row> |
|
19 |
<el-form-item label="发给谁" size="small"> |
|
20 |
<el-select v-model="sendUserId" placeholder="请选择"> |
|
21 |
<el-option label="所有人" :value="null"/> |
|
22 |
<el-option |
|
23 |
v-for="item in userList" |
|
24 |
:key="item.id" |
|
25 |
:label="item.nickname" |
|
26 |
:value="item.id"> |
|
27 |
</el-option> |
|
28 |
</el-select> |
|
29 |
</el-form-item> |
|
30 |
<el-form-item label="发送内容" size="small"> |
|
31 |
<el-input type="textarea" v-model="sendText" :rows="5"/> |
|
32 |
</el-form-item> |
|
33 |
<el-form-item label="" size="small"> |
|
34 |
<el-button type="success" @click="send">发送消息</el-button> |
|
35 |
</el-form-item> |
|
36 |
<el-form-item label="接收内容" size="small"> |
|
37 |
<el-input type="textarea" v-model="content" :rows="12" disabled/> |
|
38 |
</el-form-item> |
|
39 |
<el-form-item label="" size="small"> |
|
40 |
<el-button type="info" @click="content=''">清空消息</el-button> |
|
41 |
</el-form-item> |
|
42 |
</el-form> |
|
43 |
</div> |
|
44 |
</template> |
|
45 |
|
|
46 |
<script> |
|
47 |
import {getNowDateTime} from "@/utils/ruoyi"; |
|
48 |
import {getAccessToken} from "@/utils/auth"; |
|
49 |
import {listSimpleUsers} from "@/api/system/user"; |
|
50 |
|
|
51 |
export default { |
|
52 |
data() { |
|
53 |
return { |
|
54 |
url: "", |
|
55 |
sendText: "", |
|
56 |
content: "", |
|
57 |
ws: null, |
|
58 |
userList: [], // 用户列表 |
|
59 |
sendUserId: null // 发给谁,默认所有人 |
|
60 |
}; |
|
61 |
}, |
|
62 |
created() { |
|
63 |
const wsUrl = process.env.VUE_APP_BASE_API + "/infra/ws" + '?token=' + getAccessToken(); |
|
64 |
this.url = wsUrl.replace("http", "ws"); |
|
65 |
// 获取用户精简信息列表 |
|
66 |
listSimpleUsers().then(res => { |
|
67 |
this.userList = res.data; |
|
68 |
}); |
|
69 |
}, |
|
70 |
methods: { |
|
71 |
/** 发起连接 */ |
|
72 |
connect() { |
|
73 |
if (!'WebSocket' in window) { |
|
74 |
this.$modal.msgError("您的浏览器不支持WebSocket"); |
|
75 |
return; |
|
76 |
} |
|
77 |
// 建立连接 |
|
78 |
this.ws = new WebSocket(this.url); |
|
79 |
// 监听 open 事件 |
|
80 |
this.ws.onopen = (event) => { |
|
81 |
this.content = this.content + "\n**********************连接开始**********************\n"; |
|
82 |
}; |
|
83 |
// 监听 message 事件 |
|
84 |
this.ws.onmessage = (event) => { |
|
85 |
try { |
|
86 |
const data = event.data; |
|
87 |
// 1. 收到心跳 |
|
88 |
if (data === 'pong') { |
|
89 |
return; |
|
90 |
} |
|
91 |
// 2.1 解析 type 消息类型 |
|
92 |
const jsonMessage = JSON.parse(data) |
|
93 |
const type = jsonMessage.type; |
|
94 |
const content = JSON.parse(jsonMessage.content); |
|
95 |
if (!type) { |
|
96 |
this.$modal.msgError('未知的消息类型:' + data); |
|
97 |
return; |
|
98 |
} |
|
99 |
// 2.2 消息类型:demo-message-receive |
|
100 |
if (type === 'demo-message-receive') { |
|
101 |
const single = content.single; |
|
102 |
this.content = this.content + "接收时间:" + getNowDateTime() + "\n" + |
|
103 |
`【${single ? '单发' : '群发'}】用户编号(${content.fromUserId}):${content.text}` + "\n"; |
|
104 |
return; |
|
105 |
} |
|
106 |
// 2.3 消息类型:notice-push |
|
107 |
if (type === 'notice-push') { |
|
108 |
this.content = this.content + "接收时间:" + getNowDateTime() + "\n" + `【系统通知】:${content.title}` + "\n"; |
|
109 |
return; |
|
110 |
} |
|
111 |
this.$modal.msgError('未处理消息:' + data); |
|
112 |
} catch (error) { |
|
113 |
this.$modal.msgError('处理消息发生异常:' + event.data); |
|
114 |
console.error(error); |
|
115 |
} |
|
116 |
}; |
|
117 |
// 监听 close 事件 |
|
118 |
this.ws.onclose = (event) => { |
|
119 |
this.content = this.content + "**********************连接关闭**********************\n"; |
|
120 |
}; |
|
121 |
// 监听 error 事件 |
|
122 |
this.ws.error = (event) => { |
|
123 |
this.content = this.content + "**********************连接异常**********************\n"; |
|
124 |
}; |
|
125 |
}, |
|
126 |
/** 关闭连接 */ |
|
127 |
exit() { |
|
128 |
if (this.ws) { |
|
129 |
this.ws.close(); |
|
130 |
this.ws = null; |
|
131 |
} |
|
132 |
}, |
|
133 |
/** 发送消息 */ |
|
134 |
send() { |
|
135 |
if (!this.ws || this.ws.readyState !== 1) { |
|
136 |
this.$modal.msgError("未连接到服务器"); |
|
137 |
return; |
|
138 |
} |
|
139 |
if (!this.sendText) { |
|
140 |
this.$modal.msgError("请输入发送内容"); |
|
141 |
return; |
|
142 |
} |
|
143 |
|
|
144 |
// 1.1 先 JSON 化 message 消息内容 |
|
145 |
const messageContent = JSON.stringify({ |
|
146 |
text: this.sendText, |
|
147 |
toUserId: this.sendUserId |
|
148 |
}); |
|
149 |
// 1.2 再 JSON 化整个消息 |
|
150 |
const jsonMessage = JSON.stringify({ |
|
151 |
type: 'demo-message-send', |
|
152 |
content: messageContent |
|
153 |
}); |
|
154 |
// 2. 最后发送消息 |
|
155 |
this.ws.send(jsonMessage); |
|
156 |
this.sendText = ''; |
|
157 |
} |
|
158 |
}, |
|
159 |
}; |
|
160 |
</script> |