Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ws support #1058

Merged
merged 10 commits into from Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/decorator/src/interface.ts
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
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
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
@@ -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
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