Skip to content

Commit

Permalink
feat: add ws support (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jun 7, 2021
1 parent c2bea86 commit e73cfcb
Show file tree
Hide file tree
Showing 24 changed files with 1,322 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/decorator/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export enum MidwayFrameworkType {
MS_GRPC = '@midwayjs/grpc',
MS_RABBITMQ = '@midwayjs/rabbitmq',
WS_IO = '@midwayjs/socketio',
WSS = '',
WS = '@midwayjs/ws',
SERVERLESS_APP = '@midwayjs/serverless-app',
CUSTOM = '',
EMPTY = 'empty',
Expand Down
19 changes: 19 additions & 0 deletions packages/decorator/src/ws/webSocketEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ export function WSEmit(
};
}

export function WSBroadCast(
messageName = '',
roomName: string | string[] = []
): MethodDecorator {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
attachClassMetadata(
WS_EVENT_KEY,
{
eventType: WSEventTypeEnum.BROADCAST,
propertyName: propertyKey,
messageEventName: messageName,
roomName: [].concat(roomName),
descriptor,
},
target.constructor
);
};
}

/**
* @deprecated please use @OnWSDisConnection
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@types/amqplib": "*",
"amqplib": "*",
"socket.io": "^4.0.0",
"socket.io-client": "^4.0.0"
"socket.io-client": "^4.0.0",
"ws": "^7.4.5"
},
"dependencies": {
"@midwayjs/bootstrap": "^2.10.18",
Expand Down
16 changes: 16 additions & 0 deletions packages/mock/src/client/ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as WebSocket from 'ws';
import * as http from 'http';
import * as url from 'url';

export async function createWebSocketClient(
address: string | url.URL,
options?: WebSocket.ClientOptions | http.ClientRequestArgs
) {
const WebSocket = require('ws');
const client = new WebSocket(address, options);
return new Promise<WebSocket>(resolve => {
client.on('open', () => {
resolve(client);
});
});
}
1 change: 1 addition & 0 deletions packages/mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
export * from './client/http';
export * from './client/rabbitMQ';
export * from './client/socketio';
export * from './client/ws';

/**
* @deprecated below
Expand Down
Loading

0 comments on commit e73cfcb

Please sign in to comment.