feat(projects): add websocket demo

This commit is contained in:
Soybean
2023-06-18 22:23:42 +08:00
parent de2829fde7
commit af53ec7625
8 changed files with 140 additions and 4 deletions

View File

@ -4,3 +4,4 @@ export * from './layout';
export * from './events';
export * from './echarts';
export * from './icon';
export * from './websocket';

View 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
};
}

View File

@ -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;
}
}
});

View File

@ -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'

View File

@ -0,0 +1,7 @@
<template>
<div></div>
</template>
<script setup lang="ts"></script>
<style scoped></style>

View File

@ -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'),