@@ -11,7 +11,6 @@ export const debug = debugLib('obs-websocket-js:Socket');
11
11
12
12
export type ConnectArgs = {
13
13
address : string ;
14
- secure : boolean ;
15
14
password : string ;
16
15
} ;
17
16
@@ -26,9 +25,8 @@ export abstract class Socket extends EventEmitter<EventHandlersDataMap> {
26
25
27
26
async connect ( args : Partial < ConnectArgs > = { } ) : Promise < void > {
28
27
const parsedArgs = {
29
- address : 'localhost:4444' ,
28
+ address : 'ws:// localhost:4444' ,
30
29
password : '' ,
31
- secure : false ,
32
30
...args ,
33
31
} ;
34
32
@@ -45,7 +43,7 @@ export abstract class Socket extends EventEmitter<EventHandlersDataMap> {
45
43
}
46
44
47
45
try {
48
- await this . connect0 ( parsedArgs . address , parsedArgs . secure ) ;
46
+ await this . connect0 ( parsedArgs . address ) ;
49
47
await this . authenticate ( parsedArgs . password ) ;
50
48
} catch ( e : unknown ) {
51
49
this . socket . close ( ) ;
@@ -82,19 +80,21 @@ export abstract class Socket extends EventEmitter<EventHandlersDataMap> {
82
80
/**
83
81
* Opens a WebSocket connection to an obs-websocket server, but does not attempt any authentication.
84
82
*
85
- * @param {String } address url without ws:// or wss:// prefix.
86
- * @param {Boolean } secure whether to us ws:// or wss://
83
+ * @param {String } address url with or without ws:// or wss:// prefix.
87
84
* @returns {Promise }
88
85
* @private
89
86
* @return {Promise } on attempted creation of WebSocket connection.
90
87
*/
91
- private async connect0 ( address : string , secure : boolean ) : Promise < void > {
88
+ private async connect0 ( address : string ) : Promise < void > {
92
89
// We need to wrap this in a promise so we can resolve only when connected
93
90
return new Promise < void > ( ( resolve , reject ) : void => {
94
91
let settled = false ;
92
+ // Check if the address starts with a prefix and prepend if needed
93
+ const regex = / ^ w s s ? : \/ \/ / i;
94
+ const parsedAddress = `${ regex . test ( address ) ? '' : 'ws://' } ${ address } ` ;
95
95
96
- debug ( 'Attempting to connect to: %s (secure: %s) ' , address , secure ) ;
97
- this . socket = new WebSocket ( ( secure ? 'wss://' : 'ws://' ) + address ) ;
96
+ debug ( 'Attempting to connect to: %s' , parsedAddress ) ;
97
+ this . socket = new WebSocket ( parsedAddress ) ;
98
98
99
99
// We only handle the initial connection error.
100
100
// Beyond that, the consumer is responsible for adding their own generic `error` event listener.
0 commit comments