File tree Expand file tree Collapse file tree 2 files changed +41
-5
lines changed Expand file tree Collapse file tree 2 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -78,11 +78,18 @@ export abstract class BaseOBSWebSocket extends EventEmitter<MapValueToArgsArray<
78
78
this . emit ( 'Hello' , hello ) ;
79
79
return this . identify ( hello , password , identificationParams ) ;
80
80
} ) ( ) ,
81
- connectionClosedPromise . then ( e => {
82
- throw e ;
83
- } ) ,
84
- connectionErrorPromise . then ( e => {
85
- throw e ;
81
+ // Choose the best promise for connection error/close
82
+ // In browser connection close has close code + reason,
83
+ // while in node error event has these
84
+ new Promise < never > ( ( resolve , reject ) => {
85
+ void connectionErrorPromise . then ( e => {
86
+ if ( e . message ) {
87
+ reject ( e ) ;
88
+ }
89
+ } ) ;
90
+ void connectionClosedPromise . then ( e => {
91
+ reject ( e ) ;
92
+ } ) ;
86
93
} ) ,
87
94
] ) ;
88
95
} catch ( error : unknown ) {
Original file line number Diff line number Diff line change @@ -85,3 +85,32 @@ test('server returns wrong protocol', async t => {
85
85
86
86
await obs . disconnect ( ) ;
87
87
} ) ;
88
+
89
+ test ( 'no server listening' , async t => {
90
+ const obs = new OBSWebSocket ( ) ;
91
+
92
+ // Temporarily create a websocket server to guarantee an unused port
93
+ const port = await new Promise < number > ( resolve => {
94
+ const server = new WebSocketServer ( {
95
+ port : 0 ,
96
+ handleProtocols ( ) {
97
+ return 'dummy' ;
98
+ } ,
99
+ } , ( ) => {
100
+ const { port} = server . address ( ) as AddressInfo ;
101
+ server . close ( ( ) => {
102
+ resolve ( port ) ;
103
+ } ) ;
104
+ } ) ;
105
+ } ) ;
106
+
107
+ t . false ( obs . identified ) ;
108
+ await t . throwsAsync ( obs . connect ( `ws://127.0.0.1:${ port } ` ) , {
109
+ instanceOf : OBSWebSocketError ,
110
+ message : `connect ECONNREFUSED 127.0.0.1:${ port } ` ,
111
+ code : - 1 ,
112
+ } ) ;
113
+ t . false ( obs . identified ) ;
114
+
115
+ await obs . disconnect ( ) ;
116
+ } ) ;
You can’t perform that action at this time.
0 commit comments