File tree Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -41,5 +41,38 @@ describe('WebSocketGateway (ack)', () => {
41
41
) ;
42
42
} ) ;
43
43
44
+ it ( 'should handle manual ack for async operations when @Ack() is used (success case)' , async ( ) => {
45
+ app = await createNestApp ( AckGateway ) ;
46
+ await app . listen ( 3000 ) ;
47
+
48
+ ws = io ( 'http://localhost:8080' ) ;
49
+ const payload = { shouldSucceed : true } ;
50
+
51
+ await new Promise < void > ( resolve =>
52
+ ws . emit ( 'manual-ack' , payload , response => {
53
+ expect ( response ) . to . eql ( { status : 'success' , data : payload } ) ;
54
+ resolve ( ) ;
55
+ } ) ,
56
+ ) ;
57
+ } ) ;
58
+
59
+ it ( 'should handle manual ack for async operations when @Ack() is used (error case)' , async ( ) => {
60
+ app = await createNestApp ( AckGateway ) ;
61
+ await app . listen ( 3000 ) ;
62
+
63
+ ws = io ( 'http://localhost:8080' ) ;
64
+ const payload = { shouldSucceed : false } ;
65
+
66
+ await new Promise < void > ( resolve =>
67
+ ws . emit ( 'manual-ack' , payload , response => {
68
+ expect ( response ) . to . eql ( {
69
+ status : 'error' ,
70
+ message : 'Operation failed' ,
71
+ } ) ;
72
+ resolve ( ) ;
73
+ } ) ,
74
+ ) ;
75
+ } ) ;
76
+
44
77
afterEach ( ( ) => app . close ( ) ) ;
45
78
} ) ;
Original file line number Diff line number Diff line change 1
- import { SubscribeMessage , WebSocketGateway } from '@nestjs/websockets' ;
1
+ import {
2
+ Ack ,
3
+ MessageBody ,
4
+ SubscribeMessage ,
5
+ WebSocketGateway ,
6
+ } from '@nestjs/websockets' ;
2
7
3
8
@WebSocketGateway ( 8080 )
4
9
export class AckGateway {
5
10
@SubscribeMessage ( 'push' )
6
11
onPush ( ) {
7
12
return 'pong' ;
8
13
}
14
+
15
+ @SubscribeMessage ( 'manual-ack' )
16
+ async handleManualAck (
17
+ @MessageBody ( ) data : any ,
18
+ @Ack ( ) ack : ( response : any ) => void ,
19
+ ) {
20
+ await new Promise ( resolve => setTimeout ( resolve , 20 ) ) ;
21
+
22
+ if ( data . shouldSucceed ) {
23
+ ack ( { status : 'success' , data } ) ;
24
+ } else {
25
+ ack ( { status : 'error' , message : 'Operation failed' } ) ;
26
+ }
27
+ return { status : 'ignored' } ;
28
+ }
9
29
}
You can’t perform that action at this time.
0 commit comments