3030 断开
3131 </el-button >
3232 </el-col >
33-
3433 <el-col :span =" 8" class =" text-right" >
3534 连接状态:
3635 <el-tag class =" ml-2" type =" success" v-if =" isConnected" >
4039 </el-col >
4140 </el-row >
4241 </el-card >
43-
42+ <!-- 广播消息发送部分 -->
4443 <el-card class =" mt-5" >
4544 <el-form label-width =" 90px" >
4645 <el-form-item label =" 消息内容" >
4746 <el-input type =" textarea" v-model =" topicMessage" />
4847 </el-form-item >
49-
5048 <el-form-item >
5149 <el-button @click =" sendToAll" type =" primary" >发送广播</el-button >
5250 </el-form-item >
5351 </el-form >
5452 </el-card >
55-
53+ <!-- 点对点消息发送部分 -->
5654 <el-card class =" mt-5" >
5755 <el-form label-width =" 90px" >
5856 <el-form-item label =" 消息内容" >
6967 </el-form >
7068 </el-card >
7169 </el-col >
72-
70+ <!-- 消息接收显示部分 -->
7371 <el-col :span =" 12" >
7472 <el-card >
7573 <div class =" message-container" >
105103 </div >
106104</template >
107105
108- <!-- websocket 示例 -->
109106<script setup lang="ts">
110- import SockJS from " sockjs-client/dist/sockjs.min.js" ;
111- // import SockJS from "sockjs-client";
112- import Stomp from " stompjs" ;
107+ import { Client } from " @stomp/stompjs" ;
113108
114109import { useUserStoreHook } from " @/store/modules/user" ;
115110import { TOKEN_KEY } from " @/enums/CacheEnum" ;
116111
117112const userStore = useUserStoreHook ();
118-
119113const isConnected = ref (false );
120- // const socketEndpoint = ref("http ://47.117.115.107:8989 /ws"); // 线上
121- const socketEndpoint = ref (" http ://localhost:8989/ws" ); // 本地
114+ const socketEndpoint = ref (" https ://vue3.youlai.tech /ws" );
115+ // const socketEndpoint = ref("ws ://localhost:8989/ws");
122116
123117const receiver = ref (" root" );
124118
125119interface MessageType {
126- type? : string ; // 消息类型: tip-提示消息
120+ type? : string ;
127121 sender? : string ;
128122 content: string ;
129123}
@@ -142,40 +136,27 @@ const queneMessage = ref(
142136 " , 想和你交个朋友 ! "
143137);
144138
145- function sendToAll() {
146- stompClient .send (" /app/sendToAll" , {}, topicMessage .value );
147- messages .value .push ({
148- sender: userStore .user .username ,
149- content: topicMessage .value ,
150- });
151- }
152-
153- function sendToUser() {
154- stompClient .send (" /app/sendToUser/" + receiver .value , {}, queneMessage .value );
155- messages .value .push ({
156- sender: userStore .user .username ,
157- content: queneMessage .value ,
158- });
159- }
160-
161- let stompClient: Stomp .Client ;
139+ let stompClient: Client ;
162140
163141function connectWebSocket() {
164- let socket = new SockJS (socketEndpoint .value );
165-
166- stompClient = Stomp .over (socket );
167-
168- stompClient .connect (
169- { Authorization: localStorage .getItem (TOKEN_KEY ) },
170- () => {
142+ stompClient = new Client ({
143+ brokerURL: socketEndpoint .value ,
144+ connectHeaders: {
145+ Authorization: localStorage .getItem (TOKEN_KEY ) || " " ,
146+ },
147+ debug : (str ) => {
148+ console .log (str );
149+ },
150+ onConnect : () => {
151+ console .log (" 连接成功" );
171152 isConnected .value = true ;
172153 messages .value .push ({
173154 sender: " Server" ,
174155 content: " Websocket 已连接" ,
175156 type: " tip" ,
176157 });
177158
178- stompClient .subscribe (" /topic/notice" , (res : any ) => {
159+ stompClient .subscribe (" /topic/notice" , (res ) => {
179160 messages .value .push ({
180161 sender: " Server" ,
181162 content: res .body ,
@@ -190,27 +171,57 @@ function connectWebSocket() {
190171 });
191172 });
192173 },
193- (error ) => {
194- console .log (" 连接失败: " + error );
195- isConnected .value = false ; // 更新连接状态
174+ onStompError : (frame ) => {
175+ console .error (" Broker reported error: " + frame .headers [" message" ]);
176+ console .error (" Additional details: " + frame .body );
177+ },
178+ onDisconnect : () => {
179+ isConnected .value = false ;
196180 messages .value .push ({
197181 sender: " Server" ,
198182 content: " Websocket 已断开" ,
199183 type: " tip" ,
200184 });
201- }
202- );
185+ },
186+ });
187+
188+ stompClient .activate ();
203189}
204190
205191function disconnectWebSocket() {
206192 if (stompClient && stompClient .connected ) {
207- stompClient .disconnect (() => {
208- isConnected .value = false ; // 更新连接状态
209- messages .value .push ({
210- sender: " Server" ,
211- content: " Websocket 已断开" ,
212- type: " tip" ,
213- });
193+ stompClient .deactivate ();
194+ isConnected .value = false ;
195+ messages .value .push ({
196+ sender: " Server" ,
197+ content: " Websocket 已断开" ,
198+ type: " tip" ,
199+ });
200+ }
201+ }
202+
203+ function sendToAll() {
204+ if (stompClient .connected ) {
205+ stompClient .publish ({
206+ destination: " /topic/notice" ,
207+ body: topicMessage .value ,
208+ });
209+ messages .value .push ({
210+ sender: userStore .user .username ,
211+ content: topicMessage .value ,
212+ });
213+ }
214+ }
215+
216+ function sendToUser() {
217+ if (stompClient .connected ) {
218+ stompClient .publish ({
219+ destination: " /app/sendToUser/" + receiver .value ,
220+ body: queneMessage .value ,
221+ });
222+ messages .value .push ({
223+ sender: userStore .user .username ,
224+ content: queneMessage .value ,
214225 });
215226 }
216227}
0 commit comments