From 68cb0ee20ad19f96952e4d4746c1689d829f6bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Sat, 3 Jun 2023 11:12:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E6=96=B0=E5=A2=9E`websock?= =?UTF-8?q?et`=E6=B5=8B=E8=AF=95=E9=A1=B5=E9=9D=A2(=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E9=98=B6=E6=AE=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 8c5a49f95da85f6a245d9a3cc60d5f925cfc0861 --- src/api/user/types.ts | 1 + src/api/websocket/index.ts | 27 ++++++++ src/store/modules/user.ts | 6 ++ src/views/dashboard/index.vue | 10 +-- src/views/demo/websocket.vue | 119 ++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 src/api/websocket/index.ts create mode 100644 src/views/demo/websocket.vue diff --git a/src/api/user/types.ts b/src/api/user/types.ts index 613ad6e1..057be5f3 100644 --- a/src/api/user/types.ts +++ b/src/api/user/types.ts @@ -2,6 +2,7 @@ * 登录用户信息 */ export interface UserInfo { + userId: number; nickname: string; avatar: string; roles: string[]; diff --git a/src/api/websocket/index.ts b/src/api/websocket/index.ts new file mode 100644 index 00000000..867a2a23 --- /dev/null +++ b/src/api/websocket/index.ts @@ -0,0 +1,27 @@ +import request from "@/utils/request"; + +/** + * 发送消息给所有人 + * + * @param file + */ +export function sendToAll(message: string) { + return request({ + url: "/websocket/sendToAll", + method: "post", + params: { message: message }, + }); +} + +/** + * 发送消息给指定用户 + * + * @param + */ +export function sendToUser(userId: number, message: string) { + return request({ + url: "/websocket/sendToUser/" + userId, + method: "post", + params: { message: message }, + }); +} diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 72d806d5..d30fee09 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -12,6 +12,7 @@ import { useStorage } from "@vueuse/core"; export const useUserStore = defineStore("user", () => { // state + const userId = ref(); const token = useStorage("accessToken", ""); const nickname = ref(""); const avatar = ref(""); @@ -49,6 +50,7 @@ export const useUserStore = defineStore("user", () => { if (!data.roles || data.roles.length <= 0) { reject("getUserInfo: roles must be a non-null array!"); } + userId.value = data.userId; nickname.value = data.nickname; avatar.value = data.avatar; roles.value = data.roles; @@ -95,6 +97,10 @@ export const useUserStore = defineStore("user", () => { getInfo, logout, resetToken, + /** + * 当前登录用户ID + */ + userId, }; }); diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 359c9c87..7e1d7d73 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -1,8 +1,10 @@ - - + + + + 示例源码 请点击>>>> + + + + + + 发送点对点消息 + 发送广播消息 + + + + + + + + 点对点消息接收 + + {{ msg }} + + + + + + 广播消息接收 + + {{ msg }} + + + + + + +