210210import org .jooby .Env ;
211211import org .jooby .Jooby .Module ;
212212import org .jooby .MediaType ;
213+ import org .jooby .Request ;
213214import org .jooby .Route ;
214215import org .jooby .Router ;
215216import org .jooby .WebSocket ;
@@ -322,9 +323,9 @@ public class LiveReload implements Module {
322323 private static final Set <String > CSS = ImmutableSet .of (".css" , ".scss" , ".sass" , ".less" );
323324
324325 private Predicate <Path > css = path -> CSS .stream ()
325- .filter (ext -> path .toString ().endsWith (ext ))
326- .findFirst ()
327- .isPresent ();
326+ .filter (ext -> path .toString ().endsWith (ext ))
327+ .findFirst ()
328+ .isPresent ();
328329
329330 private List <Object []> paths = new ArrayList <>();
330331
@@ -357,23 +358,22 @@ public LiveReload liveCss(final Predicate<Path> predicate) {
357358 @ Override
358359 public void configure (final Env env , final Config conf , final Binder binder ) throws Throwable {
359360 boolean enabled = conf .hasPath ("livereload.enabled" )
360- ? conf .getBoolean ("livereload.enabled" )
361- : "dev" .equals (env .name ());
361+ ? conf .getBoolean ("livereload.enabled" )
362+ : "dev" .equals (env .name ());
362363 if (enabled ) {
363364 Router router = env .router ();
364365 /**
365366 * Livereload client:
366367 */
367368 String livereloadjs = "/" + LiveReload .class .getPackage ().getName ().replace ("." , "/" )
368- + "/livereload.js" ;
369+ + "/livereload.js" ;
369370 router .assets ("/livereload.js" , livereloadjs );
370371 /** {{liveReload}} local variable */
371- router .use ("*" , (req , rsp ) -> req .set ("liveReload" ,
372- "<script src=\" " + req .contextPath () + "/livereload.js\" ></script>" ))
373- .name ("livereload" );
372+ router .use ("*" , (req , rsp ) -> req .set ("liveReload" , template (req )))
373+ .name ("livereload" );
374374
375375 String serverName = CaseFormat .LOWER_CAMEL
376- .to (CaseFormat .UPPER_CAMEL , conf .getString ("application.name" ));
376+ .to (CaseFormat .UPPER_CAMEL , conf .getString ("application.name" ));
377377
378378 Queue <WebSocket > broadcast = new ConcurrentLinkedQueue <>();
379379 AtomicBoolean first = new AtomicBoolean (true );
@@ -411,22 +411,22 @@ public void configure(final Env env, final Config conf, final Binder binder) thr
411411 if (paths .isEmpty ()) {
412412 Path basedir = Paths .get (System .getProperty ("user.dir" ));
413413 register (basedir .resolve ("public" ),
414- "**/*.css" ,
415- "**/*.scss" ,
416- "**/*.sass" ,
417- "**/*.less" ,
418- "**/*.html" ,
419- "**/*.js" ,
420- "**/*.coffee" ,
421- "**/*.ts" );
414+ "**/*.css" ,
415+ "**/*.scss" ,
416+ "**/*.sass" ,
417+ "**/*.less" ,
418+ "**/*.html" ,
419+ "**/*.js" ,
420+ "**/*.coffee" ,
421+ "**/*.ts" );
422422 register (basedir .resolve ("target" ),
423- "**/*.class" ,
424- "**/*.conf" ,
425- "**/*.properties" );
423+ "**/*.class" ,
424+ "**/*.conf" ,
425+ "**/*.properties" );
426426 register (basedir .resolve ("build" ),
427- "**/*.class" ,
428- "**/*.conf" ,
429- "**/*.properties" );
427+ "**/*.class" ,
428+ "**/*.conf" ,
429+ "**/*.properties" );
430430 }
431431
432432 if (paths .size () > 0 ) {
@@ -453,26 +453,37 @@ public void configure(final Env env, final Config conf, final Binder binder) thr
453453 }
454454 }
455455
456+ private String template (Request req ) {
457+ String contextPath = req .contextPath ();
458+ return "<script>"
459+ + "window.LiveReloadOptions = {"
460+ + "host: '" + req .hostname () + "',"
461+ + "port: '" + req .port () + contextPath + "'"
462+ + "};"
463+ + "</script>\n "
464+ + "<script src=\" " + contextPath + "/livereload.js\" ></script>" ;
465+ }
466+
456467 private boolean isHello (final Map <String , Object > message ) {
457468 return "hello" .equals (message .get ("command" ));
458469 }
459470
460471 @ SuppressWarnings ("unchecked" )
461472 private Map <String , Object > handshake (final Map <String , Object > client ,
462- final String serverName , final String version ) {
473+ final String serverName , final String version ) {
463474 if (isHello (client )) {
464475 List <String > protocols = (List <String >) client .get ("protocols" );
465476 return protocols .stream ()
466- .filter (protocol -> version .equalsIgnoreCase (protocol ))
467- .map (protocol -> {
468- Map <String , Object > server = new LinkedHashMap <>();
469- server .put ("command" , "hello" );
470- server .put ("protocols" , new String []{protocol });
471- server .put ("serverName" , serverName );
472- return server ;
473- })
474- .findFirst ()
475- .orElse (null );
477+ .filter (protocol -> version .equalsIgnoreCase (protocol ))
478+ .map (protocol -> {
479+ Map <String , Object > server = new LinkedHashMap <>();
480+ server .put ("command" , "hello" );
481+ server .put ("protocols" , new String []{protocol });
482+ server .put ("serverName" , serverName );
483+ return server ;
484+ })
485+ .findFirst ()
486+ .orElse (null );
476487 }
477488 return null ;
478489 }
0 commit comments