Skip to content

Commit

Permalink
test(mock): update socket example code
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Dec 24, 2022
1 parent dbe7dea commit 1e7f6d0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
29 changes: 26 additions & 3 deletions packages/mock/mock/socket/sock.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 };
4 changes: 2 additions & 2 deletions packages/mock/mock/socket/sock2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const socketController = (socket) => {
const socketDefaultController = (socket) => {
const data = { a: 112 };
// 数据发送客户端
socket.emit('toClient', data);
Expand All @@ -9,4 +9,4 @@ const socketController = (socket) => {
});
};

exports.default = socketController;
exports.default = socketDefaultController;
4 changes: 2 additions & 2 deletions packages/mock/test/mock/socket/sock2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const socketController = (socket) => {
const socketDefaultController = (socket) => {
const data = { a: 112 };
// 数据发送客户端
socket.emit('toClient', data);
Expand All @@ -9,4 +9,4 @@ const socketController = (socket) => {
});
};

exports.default = socketController;
exports.default = socketDefaultController;
6 changes: 3 additions & 3 deletions packages/mock/test/mock/socket/sock3.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -11,4 +11,4 @@ const socketController = (socket: Socket) => {
});
};

export default socketController;
export default socketDefaultController;

0 comments on commit 1e7f6d0

Please sign in to comment.