mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): add websocket demo
This commit is contained in:
@ -4,3 +4,4 @@ export * from './layout';
|
||||
export * from './events';
|
||||
export * from './echarts';
|
||||
export * from './icon';
|
||||
export * from './websocket';
|
||||
|
50
src/composables/websocket.ts
Normal file
50
src/composables/websocket.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { io } from 'socket.io-client';
|
||||
import type { Socket } from 'socket.io-client';
|
||||
import { useAppStore } from '../store';
|
||||
|
||||
type ListenEvents = {
|
||||
update: (id: string, data: { name: string; age: number }) => void;
|
||||
};
|
||||
|
||||
type EmitEvents = {
|
||||
update: (id: string, data: { name: string; age: number }) => void;
|
||||
};
|
||||
|
||||
export function useWebsocket() {
|
||||
const app = useAppStore();
|
||||
|
||||
const socket: Socket<ListenEvents, EmitEvents> = (app.socket || io('ws://localhost:8080')) as Socket<
|
||||
ListenEvents,
|
||||
EmitEvents
|
||||
>;
|
||||
|
||||
if (!app.socket) {
|
||||
app.setSocket(socket);
|
||||
}
|
||||
|
||||
function init() {
|
||||
window.console.log('[socket.io] connecting...');
|
||||
|
||||
socket.on('connect', () => {
|
||||
window.console.log('[socket.io] connected.');
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
window.console.log('[socket.io] disconnected.');
|
||||
});
|
||||
|
||||
socket.on('update', (id, data) => {
|
||||
window.console.log('[socket.io] update', id, data);
|
||||
});
|
||||
}
|
||||
|
||||
function handleUpdate(id: string, data: { name: string; age: number }) {
|
||||
socket.emit('update', id, data);
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
return {
|
||||
handleUpdate
|
||||
};
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { nextTick } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import type { Socket } from 'socket.io-client';
|
||||
import { LAYOUT_SCROLL_EL_ID } from '@soybeanjs/vue-materials';
|
||||
|
||||
interface AppState {
|
||||
@ -17,6 +18,8 @@ interface AppState {
|
||||
siderCollapse: boolean;
|
||||
/** vertical-mix模式下 侧边栏的固定状态 */
|
||||
mixSiderFixed: boolean;
|
||||
/** socket.io 实例 */
|
||||
socket: Socket | null;
|
||||
}
|
||||
|
||||
export const useAppStore = defineStore('app-store', {
|
||||
@ -27,7 +30,8 @@ export const useAppStore = defineStore('app-store', {
|
||||
reloadFlag: true,
|
||||
settingDrawerVisible: false,
|
||||
siderCollapse: false,
|
||||
mixSiderFixed: false
|
||||
mixSiderFixed: false,
|
||||
socket: null
|
||||
}),
|
||||
actions: {
|
||||
/**
|
||||
@ -97,6 +101,10 @@ export const useAppStore = defineStore('app-store', {
|
||||
/** 设置主体内容全屏 */
|
||||
setContentFull(full: boolean) {
|
||||
this.contentFull = full;
|
||||
},
|
||||
/** 设置socket实例 */
|
||||
setSocket<T extends Socket = Socket>(socket: T) {
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
2
src/typings/page-route.d.ts
vendored
2
src/typings/page-route.d.ts
vendored
@ -47,6 +47,7 @@ declare namespace PageRoute {
|
||||
| 'function_tab-detail'
|
||||
| 'function_tab-multi-detail'
|
||||
| 'function_tab'
|
||||
| 'function_websocket'
|
||||
| 'management'
|
||||
| 'management_auth'
|
||||
| 'management_role'
|
||||
@ -102,6 +103,7 @@ declare namespace PageRoute {
|
||||
| 'function_tab-detail'
|
||||
| 'function_tab-multi-detail'
|
||||
| 'function_tab'
|
||||
| 'function_websocket'
|
||||
| 'management_auth'
|
||||
| 'management_role'
|
||||
| 'management_route'
|
||||
|
7
src/views/function/websocket/index.vue
Normal file
7
src/views/function/websocket/index.vue
Normal file
@ -0,0 +1,7 @@
|
||||
<template>
|
||||
<div></div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts"></script>
|
||||
|
||||
<style scoped></style>
|
@ -29,6 +29,7 @@ export const views: Record<
|
||||
'function_tab-detail': () => import('./function/tab-detail/index.vue'),
|
||||
'function_tab-multi-detail': () => import('./function/tab-multi-detail/index.vue'),
|
||||
function_tab: () => import('./function/tab/index.vue'),
|
||||
function_websocket: () => import('./function/websocket/index.vue'),
|
||||
management_auth: () => import('./management/auth/index.vue'),
|
||||
management_role: () => import('./management/role/index.vue'),
|
||||
management_route: () => import('./management/route/index.vue'),
|
||||
|
Reference in New Issue
Block a user