@@ -301,16 +301,12 @@ export class SpacetimeDBClient {
301301 private runtime : {
302302 host : string ;
303303 name_or_address : string ;
304- credentials ?: { identity : string ; token : string } ;
304+ auth_token ?: string ;
305305 global : SpacetimeDBGlobals ;
306306 } ;
307307 private createWSFn : CreateWSFnType ;
308308
309- constructor (
310- host : string ,
311- name_or_address : string ,
312- credentials ?: { identity : string ; token : string }
313- ) {
309+ constructor ( host : string , name_or_address : string , auth_token ?: string ) {
314310 const global = g . __SPACETIMEDB__ ;
315311 this . db = global . clientDB ;
316312 // I don't really like it, but it seems like the only way to
@@ -332,7 +328,7 @@ export class SpacetimeDBClient {
332328 this . runtime = {
333329 host,
334330 name_or_address,
335- credentials ,
331+ auth_token ,
336332 global,
337333 } ;
338334
@@ -361,11 +357,7 @@ export class SpacetimeDBClient {
361357 * @param name_or_address The name or address of the spacetimeDB module
362358 * @param credentials The credentials to use to connect to the spacetimeDB module
363359 */
364- public connect (
365- host ?: string ,
366- name_or_address ?: string ,
367- credentials ?: { identity : string ; token : string }
368- ) {
360+ public connect ( host ?: string , name_or_address ?: string , auth_token ?: string ) {
369361 if ( this . live ) {
370362 return ;
371363 }
@@ -380,14 +372,13 @@ export class SpacetimeDBClient {
380372 this . runtime . name_or_address = name_or_address ;
381373 }
382374
383- if ( credentials ) {
384- this . runtime . credentials = credentials ;
375+ if ( auth_token ) {
376+ this . runtime . auth_token = auth_token ;
385377 }
386378
387379 let headers : { [ key : string ] : string } = { } ;
388- if ( this . runtime . credentials ) {
389- this . identity = this . runtime . credentials . identity ;
390- this . token = this . runtime . credentials . token ;
380+ if ( this . runtime . auth_token ) {
381+ this . token = this . runtime . auth_token ;
391382 headers [ "Authorization" ] = `Basic ${ btoa ( "token:" + this . token ) } ` ;
392383 }
393384 let url = `${ this . runtime . host } /database/subscribe/${ this . runtime . name_or_address } ` ;
@@ -400,6 +391,14 @@ export class SpacetimeDBClient {
400391
401392 this . ws = this . createWSFn ( url , headers , "v1.text.spacetimedb" ) ;
402393
394+ // Create a timeout for the connection to be established
395+ var connectionTimeout = setTimeout ( ( ) => {
396+ this . ws . close ( ) ;
397+ console . error ( "Connect failed: timeout" ) ;
398+ this . emitter . emit ( "disconnected" ) ;
399+ this . emitter . emit ( "client_error" , event ) ;
400+ } , 5000 ) ; // 5000 ms = 5 seconds
401+
403402 this . ws . onclose = ( event ) => {
404403 console . error ( "Closed: " , event ) ;
405404 this . emitter . emit ( "disconnected" ) ;
@@ -413,6 +412,8 @@ export class SpacetimeDBClient {
413412 } ;
414413
415414 this . ws . onopen = ( ) => {
415+ clearTimeout ( connectionTimeout ) ;
416+
416417 this . live = true ;
417418
418419 if ( this . queriesQueue . length > 0 ) {
@@ -490,7 +491,7 @@ export class SpacetimeDBClient {
490491 const token = identityToken [ "token" ] ;
491492 this . identity = identity ;
492493 this . token = token ;
493- this . emitter . emit ( "connected" , identity ) ;
494+ this . emitter . emit ( "connected" , token , identity ) ;
494495 }
495496 }
496497 } ;
@@ -570,6 +571,10 @@ export class SpacetimeDBClient {
570571 this . on ( "connected" , callback ) ;
571572 }
572573
574+ onError ( callback : ( ...args : any [ ] ) => void ) {
575+ this . on ( "client_error" , callback ) ;
576+ }
577+
573578 _setCreateWSFn ( fn : CreateWSFnType ) {
574579 this . createWSFn = fn ;
575580 }
0 commit comments