fix: 🐛 websocket部署无法连接
This commit is contained in:
@@ -57,15 +57,13 @@
|
|||||||
"element-plus": "^2.8.0",
|
"element-plus": "^2.8.0",
|
||||||
"exceljs": "^4.4.0",
|
"exceljs": "^4.4.0",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"net": "^1.0.2",
|
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"path-browserify": "^1.0.1",
|
"path-browserify": "^1.0.1",
|
||||||
"path-to-regexp": "^6.2.2",
|
"path-to-regexp": "^6.2.2",
|
||||||
"pinia": "^2.2.2",
|
"pinia": "^2.2.2",
|
||||||
"qs": "^6.13.0",
|
"qs": "^6.13.0",
|
||||||
"sockjs-client": "1.6.1",
|
|
||||||
"sortablejs": "^1.15.2",
|
"sortablejs": "^1.15.2",
|
||||||
"stompjs": "^2.3.3",
|
"@stomp/stompjs": "^7.0.0",
|
||||||
"vue": "^3.4.38",
|
"vue": "^3.4.38",
|
||||||
"vue-i18n": "9.9.1",
|
"vue-i18n": "9.9.1",
|
||||||
"vue-router": "^4.4.3"
|
"vue-router": "^4.4.3"
|
||||||
@@ -81,7 +79,6 @@
|
|||||||
"@types/nprogress": "^0.2.3",
|
"@types/nprogress": "^0.2.3",
|
||||||
"@types/path-browserify": "^1.0.2",
|
"@types/path-browserify": "^1.0.2",
|
||||||
"@types/qs": "^6.9.15",
|
"@types/qs": "^6.9.15",
|
||||||
"@types/sockjs-client": "^1.5.4",
|
|
||||||
"@types/sortablejs": "^1.15.8",
|
"@types/sortablejs": "^1.15.8",
|
||||||
"@types/stompjs": "^2.3.9",
|
"@types/stompjs": "^2.3.9",
|
||||||
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
||||||
|
|||||||
@@ -30,7 +30,6 @@
|
|||||||
断开
|
断开
|
||||||
</el-button>
|
</el-button>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
|
||||||
<el-col :span="8" class="text-right">
|
<el-col :span="8" class="text-right">
|
||||||
连接状态:
|
连接状态:
|
||||||
<el-tag class="ml-2" type="success" v-if="isConnected">
|
<el-tag class="ml-2" type="success" v-if="isConnected">
|
||||||
@@ -40,19 +39,18 @@
|
|||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
<!-- 广播消息发送部分 -->
|
||||||
<el-card class="mt-5">
|
<el-card class="mt-5">
|
||||||
<el-form label-width="90px">
|
<el-form label-width="90px">
|
||||||
<el-form-item label="消息内容">
|
<el-form-item label="消息内容">
|
||||||
<el-input type="textarea" v-model="topicMessage" />
|
<el-input type="textarea" v-model="topicMessage" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-button @click="sendToAll" type="primary">发送广播</el-button>
|
<el-button @click="sendToAll" type="primary">发送广播</el-button>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
<!-- 点对点消息发送部分 -->
|
||||||
<el-card class="mt-5">
|
<el-card class="mt-5">
|
||||||
<el-form label-width="90px">
|
<el-form label-width="90px">
|
||||||
<el-form-item label="消息内容">
|
<el-form-item label="消息内容">
|
||||||
@@ -69,7 +67,7 @@
|
|||||||
</el-form>
|
</el-form>
|
||||||
</el-card>
|
</el-card>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
<!-- 消息接收显示部分 -->
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
<el-card>
|
<el-card>
|
||||||
<div class="message-container">
|
<div class="message-container">
|
||||||
@@ -105,25 +103,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<!-- websocket 示例 -->
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import SockJS from "sockjs-client/dist/sockjs.min.js";
|
import { Client } from "@stomp/stompjs";
|
||||||
//import SockJS from "sockjs-client";
|
|
||||||
import Stomp from "stompjs";
|
|
||||||
|
|
||||||
import { useUserStoreHook } from "@/store/modules/user";
|
import { useUserStoreHook } from "@/store/modules/user";
|
||||||
import { TOKEN_KEY } from "@/enums/CacheEnum";
|
import { TOKEN_KEY } from "@/enums/CacheEnum";
|
||||||
|
|
||||||
const userStore = useUserStoreHook();
|
const userStore = useUserStoreHook();
|
||||||
|
|
||||||
const isConnected = ref(false);
|
const isConnected = ref(false);
|
||||||
// const socketEndpoint = ref("http://47.117.115.107:8989/ws"); // 线上
|
const socketEndpoint = ref("https://vue3.youlai.tech/ws");
|
||||||
const socketEndpoint = ref("http://localhost:8989/ws"); // 本地
|
//const socketEndpoint = ref("ws://localhost:8989/ws");
|
||||||
|
|
||||||
const receiver = ref("root");
|
const receiver = ref("root");
|
||||||
|
|
||||||
interface MessageType {
|
interface MessageType {
|
||||||
type?: string; // 消息类型: tip-提示消息
|
type?: string;
|
||||||
sender?: string;
|
sender?: string;
|
||||||
content: string;
|
content: string;
|
||||||
}
|
}
|
||||||
@@ -142,32 +136,19 @@ const queneMessage = ref(
|
|||||||
" , 想和你交个朋友 ! "
|
" , 想和你交个朋友 ! "
|
||||||
);
|
);
|
||||||
|
|
||||||
function sendToAll() {
|
let stompClient: Client;
|
||||||
stompClient.send("/app/sendToAll", {}, topicMessage.value);
|
|
||||||
messages.value.push({
|
|
||||||
sender: userStore.user.username,
|
|
||||||
content: topicMessage.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendToUser() {
|
|
||||||
stompClient.send("/app/sendToUser/" + receiver.value, {}, queneMessage.value);
|
|
||||||
messages.value.push({
|
|
||||||
sender: userStore.user.username,
|
|
||||||
content: queneMessage.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let stompClient: Stomp.Client;
|
|
||||||
|
|
||||||
function connectWebSocket() {
|
function connectWebSocket() {
|
||||||
let socket = new SockJS(socketEndpoint.value);
|
stompClient = new Client({
|
||||||
|
brokerURL: socketEndpoint.value,
|
||||||
stompClient = Stomp.over(socket);
|
connectHeaders: {
|
||||||
|
Authorization: localStorage.getItem(TOKEN_KEY) || "",
|
||||||
stompClient.connect(
|
},
|
||||||
{ Authorization: localStorage.getItem(TOKEN_KEY) },
|
debug: (str) => {
|
||||||
() => {
|
console.log(str);
|
||||||
|
},
|
||||||
|
onConnect: () => {
|
||||||
|
console.log("连接成功");
|
||||||
isConnected.value = true;
|
isConnected.value = true;
|
||||||
messages.value.push({
|
messages.value.push({
|
||||||
sender: "Server",
|
sender: "Server",
|
||||||
@@ -175,7 +156,7 @@ function connectWebSocket() {
|
|||||||
type: "tip",
|
type: "tip",
|
||||||
});
|
});
|
||||||
|
|
||||||
stompClient.subscribe("/topic/notice", (res: any) => {
|
stompClient.subscribe("/topic/notice", (res) => {
|
||||||
messages.value.push({
|
messages.value.push({
|
||||||
sender: "Server",
|
sender: "Server",
|
||||||
content: res.body,
|
content: res.body,
|
||||||
@@ -190,27 +171,57 @@ function connectWebSocket() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
(error) => {
|
onStompError: (frame) => {
|
||||||
console.log("连接失败: " + error);
|
console.error("Broker reported error: " + frame.headers["message"]);
|
||||||
isConnected.value = false; // 更新连接状态
|
console.error("Additional details: " + frame.body);
|
||||||
|
},
|
||||||
|
onDisconnect: () => {
|
||||||
|
isConnected.value = false;
|
||||||
|
messages.value.push({
|
||||||
|
sender: "Server",
|
||||||
|
content: "Websocket 已断开",
|
||||||
|
type: "tip",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
stompClient.activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
function disconnectWebSocket() {
|
||||||
|
if (stompClient && stompClient.connected) {
|
||||||
|
stompClient.deactivate();
|
||||||
|
isConnected.value = false;
|
||||||
messages.value.push({
|
messages.value.push({
|
||||||
sender: "Server",
|
sender: "Server",
|
||||||
content: "Websocket 已断开",
|
content: "Websocket 已断开",
|
||||||
type: "tip",
|
type: "tip",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function disconnectWebSocket() {
|
function sendToAll() {
|
||||||
if (stompClient && stompClient.connected) {
|
if (stompClient.connected) {
|
||||||
stompClient.disconnect(() => {
|
stompClient.publish({
|
||||||
isConnected.value = false; // 更新连接状态
|
destination: "/topic/notice",
|
||||||
messages.value.push({
|
body: topicMessage.value,
|
||||||
sender: "Server",
|
|
||||||
content: "Websocket 已断开",
|
|
||||||
type: "tip",
|
|
||||||
});
|
});
|
||||||
|
messages.value.push({
|
||||||
|
sender: userStore.user.username,
|
||||||
|
content: topicMessage.value,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendToUser() {
|
||||||
|
if (stompClient.connected) {
|
||||||
|
stompClient.publish({
|
||||||
|
destination: "/app/sendToUser/" + receiver.value,
|
||||||
|
body: queneMessage.value,
|
||||||
|
});
|
||||||
|
messages.value.push({
|
||||||
|
sender: userStore.user.username,
|
||||||
|
content: queneMessage.value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,7 +207,8 @@ export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
|
|||||||
"element-plus/es/components/badge/style/css",
|
"element-plus/es/components/badge/style/css",
|
||||||
"element-plus/es/components/steps/style/css",
|
"element-plus/es/components/steps/style/css",
|
||||||
"element-plus/es/components/step/style/css",
|
"element-plus/es/components/step/style/css",
|
||||||
"element-plus/es/components/avatar/style/cs",
|
"element-plus/es/components/avatar/style/css",
|
||||||
|
"sockjs-client/dist/sockjs.min.js",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
// 构建配置
|
// 构建配置
|
||||||
|
|||||||
Reference in New Issue
Block a user