From ffee229dd014b8085bcd16ff8e36d22851692b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Thu, 7 Sep 2023 18:47:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E9=9B=86=E6=88=90`websock?= =?UTF-8?q?et`=E5=8F=91=E9=80=81=E5=92=8C=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: ab14ea128a072a2a6b61b9654360f25767f5d18c --- package.json | 3 ++- src/views/demo/websocket.vue | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ecc9202..0c9a9d7 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "element-plus": "^2.3.12", "lodash-es": "^4.17.21", "mockjs": "^1.1.0", + "net": "^1.0.2", "nprogress": "^0.2.0", "path-browserify": "^1.0.1", "path-to-regexp": "^6.2.0", @@ -57,8 +58,8 @@ "screenfull": "^6.0.0", "sockjs-client": "1.6.1", "sortablejs": "^1.15.0", - "terser": "^5.19.3", "stompjs": "^2.3.3", + "terser": "^5.19.3", "vue": "^3.3.4", "vue-i18n": "9.2.2", "vue-router": "^4.2.0", diff --git a/src/views/demo/websocket.vue b/src/views/demo/websocket.vue index ffbc4c7..eb4771d 100644 --- a/src/views/demo/websocket.vue +++ b/src/views/demo/websocket.vue @@ -4,8 +4,8 @@ import { sendToAll, sendToUser } from "@/api/websocket"; // 点对点消息列 import { useUserStore } from "@/store/modules/user"; -import SockJS from "sockjs-client"; // 报错 global is not defined 换成下面的引入 -// import SockJS from "sockjs-client/dist/sockjs.min.js"; +// import SockJS from "sockjs-client"; // 报错 global is not defined 换成下面的引入 +import SockJS from "sockjs-client/dist/sockjs.min.js"; import Stomp from "stompjs"; const inputVal = ref("初始内容"); @@ -17,6 +17,8 @@ const userId = useUserStore().userId; function handleSendToAll() { sendToAll(inputVal.value); + + stompClient.send("/app/sendToAll", {}, inputVal.value); } function handleSendToUser() { @@ -26,7 +28,9 @@ function handleSendToUser() { let stompClient: Stomp.Client; function initWebSocket() { - stompClient = Stomp.overWS("ws://localhost:8989/ws"); + let socket = new SockJS("http://localhost:8989/ws"); + + stompClient = Stomp.over(socket); stompClient.connect({}, () => { console.log("连接成功");