@@ -11,6 +11,7 @@ import AttrPlugin from 'gsap/AttrPlugin';
1111import { Power3 } from 'gsap/EasePack' ;
1212import TweenLite from 'gsap/TweenLite' ;
1313import TimelineLite from 'gsap/TimelineLite' ;
14+ import axios from 'axios' ;
1415
1516import { isStandalone } from 'codesandbox-api'
1617import getTemplate from '@codesandbox/common/lib/templates' ;
@@ -36,10 +37,11 @@ if (process.env.NODE_ENV === 'development') {
3637 hostParts = window . location . hostname . split ( '.' ) ;
3738}
3839const rootDomain = `codesandbox.${ hostParts [ hostParts . length - 1 ] } ` ;
39- const domain = `sse.${ rootDomain } ` ;
40- // parses sandboxid [-port]
41- const sandbox = hostParts [ 0 ] . replace ( / - \d + / , '' ) ;
40+ const sseLbHost = `sse-lb .${ rootDomain } ` ;
41+ // parses sandboxId [-port]
42+ const sandboxId = hostParts [ 0 ] . replace ( / - \d + / , '' ) ;
4243const port = hostParts [ 0 ] . replace ( / ^ \w + - ? / , '' ) ;
44+ // console.log('sandboxId:', sandboxId, 'port:', port);
4345const lastLoadedAt = parseInt ( localStorage . getItem ( 'last_loaded_at' ) , 10 ) ;
4446const now = Date . now ( ) ;
4547let isLoop = false ;
@@ -236,18 +238,18 @@ async function start() {
236238
237239 term . fit ( ) ;
238240
239- const socket = io ( `https://${ domain } ` , {
241+ const res = await axios . get ( `https://${ sseLbHost } /api/cluster/${ sandboxId } ` ) ;
242+ const sseHost = res . data . hostname ;
243+
244+ const socket = io ( `https://${ sseHost } ` , {
240245 autoConnect : false ,
241246 transports : [ 'websocket' ] ,
242247 reconnectionAttempts : 5 ,
243248 reconnectionDelayMax : 32000 ,
244- query : {
245- sandboxid : sandbox ,
246- } ,
247249 } ) ;
248250
249251 socket . on ( 'connect' , ( ) => {
250- socket . emit ( 'sandbox' , { id : sandbox } ) ;
252+ socket . emit ( 'sandbox' , { id : sandboxId } ) ;
251253 socket . emit ( 'sandbox:start' ) ;
252254 } ) ;
253255
@@ -258,7 +260,7 @@ async function start() {
258260 }
259261 reloadTimeout = setTimeout ( ( ) => {
260262 window . location . reload ( true ) ;
261- } , 10 * SECOND ) ;
263+ } , 15 * SECOND ) ;
262264 } ) ;
263265
264266 socket . on ( 'sandbox:error' , ( { message, unrecoverable } ) => {
@@ -341,11 +343,11 @@ if (isLoop) {
341343 localStorage . setItem ( 'last_loaded_at' , now ) ;
342344
343345 if ( process . env . NODE_ENV === 'production' ) {
344- fetch ( `https://${ rootDomain } /api/v1/sandboxes/${ sandbox } /slim` )
346+ fetch ( `https://${ rootDomain } /api/v1/sandboxes/${ sandboxId } /slim` )
345347 . then ( res => {
346348 if ( res . status === 404 ) {
347349 if ( isStandalone ) {
348- show404 ( sandbox ) ;
350+ show404 ( sandboxId ) ;
349351 }
350352
351353 return { } ;
@@ -355,7 +357,7 @@ if (isLoop) {
355357 } )
356358 . then ( json => {
357359 if ( Object . keys ( json ) > 0 && ! json . is_sse ) {
358- window . location . replace ( `https://${ sandbox } .${ rootDomain } /` ) ;
360+ window . location . replace ( `https://${ sandboxId } .${ rootDomain } /` ) ;
359361 }
360362 if ( json . template ) {
361363 const templateDef = getTemplate ( json . template ) ;
0 commit comments