feat: 集成websocket发送和订阅

Former-commit-id: ab14ea128a072a2a6b61b9654360f25767f5d18c
This commit is contained in:
郝先瑞
2023-09-07 18:47:12 +08:00
parent 18ceb2e86d
commit ffee229dd0
2 changed files with 9 additions and 4 deletions

View File

@@ -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("连接成功");