From 1e7f6d02f52b4d8b5abe436a588ed5e6442c082c Mon Sep 17 00:00:00 2001 From: liangsky <640634387@qq.com> Date: Sat, 24 Dec 2022 21:24:22 +0800 Subject: [PATCH] test(mock): update socket example code --- packages/mock/mock/socket/sock.ts | 29 ++++++++++++++++++++++--- packages/mock/mock/socket/sock2.js | 4 ++-- packages/mock/test/mock/socket/sock2.js | 4 ++-- packages/mock/test/mock/socket/sock3.ts | 6 ++--- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/mock/mock/socket/sock.ts b/packages/mock/mock/socket/sock.ts index 72fb335..f7e0673 100644 --- a/packages/mock/mock/socket/sock.ts +++ b/packages/mock/mock/socket/sock.ts @@ -1,7 +1,10 @@ -import type { Socket } from 'socket.io'; import mockjs from 'mockjs'; +import type { + ISocketDefaultController, + ISocketNamespaceController, +} from '../../src'; -const socketController = (socket: Socket) => { +const socketDefaultController: ISocketDefaultController = (socket) => { const data = mockjs.mock({ 'list|2': [{ name: '@city', 'value|1-100': 50, 'type|0-2': 1 }], a: '1', @@ -20,4 +23,24 @@ const socketController = (socket: Socket) => { }); }; -export default socketController; +export default socketDefaultController; + +const socketNamespaceController: ISocketNamespaceController = () => { + return { + '/namespace': (socket) => { + socket.emit('toClient', { data: 'init' }); + + // 定时推送数据 + let toClient = 1; + setInterval(() => { + socket.emit('toClient', toClient++); + }, 5000); + + // 接收客户端数据 + socket.on('toServer', (clientData) => { + console.log('from client:', clientData); + }); + }, + }; +}; +export { socketNamespaceController }; diff --git a/packages/mock/mock/socket/sock2.js b/packages/mock/mock/socket/sock2.js index f4e5ff8..635dcff 100644 --- a/packages/mock/mock/socket/sock2.js +++ b/packages/mock/mock/socket/sock2.js @@ -1,4 +1,4 @@ -const socketController = (socket) => { +const socketDefaultController = (socket) => { const data = { a: 112 }; // 数据发送客户端 socket.emit('toClient', data); @@ -9,4 +9,4 @@ const socketController = (socket) => { }); }; -exports.default = socketController; +exports.default = socketDefaultController; diff --git a/packages/mock/test/mock/socket/sock2.js b/packages/mock/test/mock/socket/sock2.js index f4e5ff8..635dcff 100644 --- a/packages/mock/test/mock/socket/sock2.js +++ b/packages/mock/test/mock/socket/sock2.js @@ -1,4 +1,4 @@ -const socketController = (socket) => { +const socketDefaultController = (socket) => { const data = { a: 112 }; // 数据发送客户端 socket.emit('toClient', data); @@ -9,4 +9,4 @@ const socketController = (socket) => { }); }; -exports.default = socketController; +exports.default = socketDefaultController; diff --git a/packages/mock/test/mock/socket/sock3.ts b/packages/mock/test/mock/socket/sock3.ts index 2cd389d..6e41703 100644 --- a/packages/mock/test/mock/socket/sock3.ts +++ b/packages/mock/test/mock/socket/sock3.ts @@ -1,6 +1,6 @@ -import type { Socket } from 'socket.io'; +import type { ISocketDefaultController } from '../../../src'; -const socketController = (socket: Socket) => { +const socketDefaultController: ISocketDefaultController = (socket) => { const data = { a: 112 }; // 数据发送客户端 socket.emit('toClient', data); @@ -11,4 +11,4 @@ const socketController = (socket: Socket) => { }); }; -export default socketController; +export default socketDefaultController;