-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@artusx/plugin-socketio): support nsp and event decorator
- Loading branch information
Showing
10 changed files
with
148 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Inject } from '@artusx/core'; | ||
import { PluginInjectEnum } from '@artusx/utils'; | ||
import { SocketIOServer, Namespace, Event } from '@artusx/plugin-socketio'; | ||
|
||
@Namespace('/') | ||
export default class DefaultNamespace { | ||
@Inject(PluginInjectEnum.SocketIO) | ||
io: SocketIOServer; | ||
|
||
@Event('disconnect') | ||
disconnect() { | ||
console.log('user disconnected'); | ||
} | ||
|
||
@Event('chat_message') | ||
chat_message(msg: any) { | ||
console.log('chat_message.msg', msg); | ||
this.io.emit('chat_message', msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { addTag, Injectable, ScopeEnum } from '@artus/core'; | ||
import { EventMetadata, NamespaceMetadata } from './types'; | ||
|
||
export const CLASS_NAMESPACE_TAG = 'CLASS_NAMESPACE_TAG'; | ||
export const CLASS_NAMESPACE_METADATA = Symbol.for('CLASS_NAMESPACE_METADATA'); | ||
export const EVENT_METADATA = Symbol.for('EVENT_METADATA'); | ||
|
||
export function Namespace(name: string) { | ||
return (target: any) => { | ||
const namespaceMetadata: NamespaceMetadata = { | ||
name, | ||
}; | ||
|
||
Reflect.defineMetadata(CLASS_NAMESPACE_METADATA, namespaceMetadata, target); | ||
addTag(CLASS_NAMESPACE_TAG, target); | ||
Injectable({ scope: ScopeEnum.EXECUTION })(target); | ||
}; | ||
} | ||
|
||
export function Event(name: string) { | ||
return (_target: object, _key: string, descriptor: TypedPropertyDescriptor<any>) => { | ||
const eventMetadata: EventMetadata = { | ||
name, | ||
}; | ||
|
||
Reflect.defineMetadata(EVENT_METADATA, eventMetadata, descriptor.value); | ||
return descriptor; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './server'; | ||
export * from './lifecycle'; | ||
export * from './decorator'; | ||
export * from './server'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { ServerOptions } from 'socket.io'; | ||
|
||
export type SocketIOServerConfig = Partial<ServerOptions>; | ||
|
||
export type NamespaceMetadata = { | ||
name: string; | ||
}; | ||
|
||
export type EventMetadata = { | ||
name: string; | ||
}; | ||
|
||
export type NamespaceEvent = { | ||
eventMetadata: EventMetadata; | ||
handler: (...args: any[]) => void; | ||
}; |