Mercurial Hosting > luan
changeset 1740:c9c974817d0c
add ServerSentEvents.sweep
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2022 22:05:11 -0600 |
parents | 3e9f26404433 |
children | 623d9f62ab92 |
files | src/goodjava/webserver/ServerSentEvents.java |
diffstat | 1 files changed, 29 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/goodjava/webserver/ServerSentEvents.java Thu Nov 03 19:32:03 2022 -0600 +++ b/src/goodjava/webserver/ServerSentEvents.java Thu Nov 03 22:05:11 2022 -0600 @@ -11,9 +11,14 @@ import java.util.HashMap; import java.util.Collections; import java.util.Iterator; +import java.util.Timer; +import java.util.TimerTask; +import goodjava.logging.Logger; +import goodjava.logging.LoggerFactory; public class ServerSentEvents { + private static final Logger logger = LoggerFactory.getLogger(ServerSentEvents.class); private static class Con { final Socket socket; @@ -45,8 +50,10 @@ iter.remove(); } } - if( cons.isEmpty() ) + if( cons.isEmpty() ) { map.remove(url); + //logger.info("removed "+url); + } } } @@ -92,5 +99,26 @@ write( url, toData(message) ); } + private static void sweep() { + List<String> urls; + synchronized(map) { + urls = new ArrayList<String>(map.keySet()); + } + for( String url : urls ) { + write( url, "event: ping\n" ); + } + } + + private static final Timer sweeper = new Timer("ServerSentEvents",true); + static { + long period = 1000L*60*60; // hour + TimerTask task = new TimerTask() { + public void run() { + sweep(); + } + }; + sweeper.schedule(task,period,period); + } + private ServerSentEvents() {} // never }