fix: 🐛 `sockjs-client` 报错 global is not defined 问题修复

Former-commit-id: 04496478a8
This commit is contained in:
郝先瑞 2023-09-11 18:18:36 +08:00
parent 9014c8f709
commit d1bfa599d6
2 changed files with 7 additions and 8 deletions

View File

@ -12,5 +12,8 @@
<body> <body>
<div id="app" class="app"></div> <div id="app" class="app"></div>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
<script>
global = globalThis;
</script>
</body> </body>
</html> </html>

View File

@ -1,9 +1,7 @@
<!-- websocket 示例 --> <!-- websocket 示例 -->
<script setup lang="ts"> <script setup lang="ts">
import { useUserStore } from "@/store/modules/user"; // https://github.com/sockjs/sockjs-client/issues/547 global is not defined
import SockJS from "sockjs-client";
// import SockJS from "sockjs-client"; // global is not defined
import SockJS from "sockjs-client/dist/sockjs.min.js";
import Stomp from "stompjs"; import Stomp from "stompjs";
const inputVal = ref("初始内容"); const inputVal = ref("初始内容");
@ -11,14 +9,12 @@ const inputVal = ref("初始内容");
const topicMsgs = ref<string[]>(["接收到一条主题消息"]); // const topicMsgs = ref<string[]>(["接收到一条主题消息"]); //
const p2pMsgs = ref<string[]>(["接收到一条点对线消息"]); const p2pMsgs = ref<string[]>(["接收到一条点对线消息"]);
const userId = useUserStore().userId;
function handleSendToAll() { function handleSendToAll() {
stompClient.send("/app/sendToAll", {}, inputVal.value); stompClient.send("/app/sendToAll", {}, inputVal.value);
} }
function handleSendToUser() { function handleSendToUser() {
stompClient.send("/app/sendToUser/" + userId, {}, inputVal.value); stompClient.send("/app/sendToUser", {}, inputVal.value);
} }
let stompClient: Stomp.Client; let stompClient: Stomp.Client;
@ -35,7 +31,7 @@ function initWebSocket() {
console.log("广播消息接收", res); console.log("广播消息接收", res);
}); });
stompClient.subscribe("/queue/user", (res) => { stompClient.subscribe("/user/queue/reply", (res) => {
console.log("点对点消息接收", res); console.log("点对点消息接收", res);
}); });
}); });