Skip to content

Commit b1d93da

Browse files
committed
fix: 🐛 websocket部署无法连接
1 parent ce7b9d3 commit b1d93da

File tree

3 files changed

+64
-55
lines changed

3 files changed

+64
-55
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@
5757
"element-plus": "^2.8.0",
5858
"exceljs": "^4.4.0",
5959
"lodash-es": "^4.17.21",
60-
"net": "^1.0.2",
6160
"nprogress": "^0.2.0",
6261
"path-browserify": "^1.0.1",
6362
"path-to-regexp": "^6.2.2",
6463
"pinia": "^2.2.2",
6564
"qs": "^6.13.0",
66-
"sockjs-client": "1.6.1",
6765
"sortablejs": "^1.15.2",
68-
"stompjs": "^2.3.3",
66+
"@stomp/stompjs": "^7.0.0",
6967
"vue": "^3.4.38",
7068
"vue-i18n": "9.9.1",
7169
"vue-router": "^4.4.3"
@@ -81,7 +79,6 @@
8179
"@types/nprogress": "^0.2.3",
8280
"@types/path-browserify": "^1.0.2",
8381
"@types/qs": "^6.9.15",
84-
"@types/sockjs-client": "^1.5.4",
8582
"@types/sortablejs": "^1.15.8",
8683
"@types/stompjs": "^2.3.9",
8784
"@typescript-eslint/eslint-plugin": "^7.18.0",

src/views/demo/websocket.vue

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
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">
@@ -40,19 +39,18 @@
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="消息内容">
@@ -69,7 +67,7 @@
6967
</el-form>
7068
</el-card>
7169
</el-col>
72-
70+
<!-- 消息接收显示部分 -->
7371
<el-col :span="12">
7472
<el-card>
7573
<div class="message-container">
@@ -105,25 +103,21 @@
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
114109
import { useUserStoreHook } from "@/store/modules/user";
115110
import { TOKEN_KEY } from "@/enums/CacheEnum";
116111
117112
const userStore = useUserStoreHook();
118-
119113
const 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
123117
const receiver = ref("root");
124118
125119
interface 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
163141
function 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
205191
function 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
}

vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
207207
"element-plus/es/components/badge/style/css",
208208
"element-plus/es/components/steps/style/css",
209209
"element-plus/es/components/step/style/css",
210-
"element-plus/es/components/avatar/style/cs",
210+
"element-plus/es/components/avatar/style/css",
211+
"sockjs-client/dist/sockjs.min.js",
211212
],
212213
},
213214
// 构建配置

0 commit comments

Comments
 (0)